From 9beafe1c05d1d703eb80a255c6a1b254e701955e Mon Sep 17 00:00:00 2001 From: Jan Frenzel <jan.frenzel@tu-dresden.de> Date: Tue, 20 Aug 2024 16:43:13 +0200 Subject: [PATCH] Add base example --- base/README.md | 28 ++++++++++++++++++++++++++++ base/main.py | 30 ++++++++++++++++++++++++++++++ base/requirements.txt | 1 + 3 files changed, 59 insertions(+) create mode 100644 base/README.md create mode 100755 base/main.py create mode 100644 base/requirements.txt diff --git a/base/README.md b/base/README.md new file mode 100644 index 0000000..15512e3 --- /dev/null +++ b/base/README.md @@ -0,0 +1,28 @@ +# Base functionality + +This example intends to show you how to: + +- list models +- select and use the first model with "llama" in its name to display a joke + +## Installation + +Do the following steps in your bash shell: + +```bash +python3 -m venv myenv +source myenv/bin/activate +pip install -r requirements.txt +``` + +## Usage + +1. Put your API key in the file `my_key`. +2. Then simply start the script `main.py` from your bash shell: + +```bash +source myenv/bin/activate +./main.py +``` + +3. Read the joke. diff --git a/base/main.py b/base/main.py new file mode 100755 index 0000000..330c28c --- /dev/null +++ b/base/main.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# Find instructions how to install dependencies and how to run this script in README.md +from openai import OpenAI +my_api_key = "" +with open("my_key") as keyfile: + my_api_key = keyfile.readline()[:-1] + +client = OpenAI(base_url="https://llm.scads.ai/v1",api_key=my_api_key) +# Get models +print(""" +Available models: +""") +for model in client.models.list().data: + print(model.id) + +# Find model with "llama" in name +for model in client.models.list().data: + model_name = model.id + if "llama" in model_name: + break + +# Use model +response = client.chat.completions.create(messages=[{"role":"user","content":"Tell me a joke!"}],model=model_name) + +# Print the joke +print(""" +Your joke: +""") +joke = response.choices[0].message.content +print(joke) diff --git a/base/requirements.txt b/base/requirements.txt new file mode 100644 index 0000000..ec838c5 --- /dev/null +++ b/base/requirements.txt @@ -0,0 +1 @@ +openai -- GitLab