Skip to content
Snippets Groups Projects
Commit b642af65 authored by Jan Frenzel's avatar Jan Frenzel
Browse files

Merge branch 'base-example' into 'main'

Base example

See merge request !1
parents bcd337e0 d408b816
No related branches found
No related tags found
1 merge request!1Base example
......@@ -6,7 +6,7 @@ This repository contains code snippets to get you started with our API. Before y
Each example comes with its own installation instructions, because there are examples with several languages and environments which makes this necessary.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
A base example is available in directory [base](base).
## Support
If you have the feeling that the examples contain errors or otherwise need improvement, please [check whether there is an open issues already](https://gitlab.hrz.tu-chemnitz.de/scads-ai-llm/scads-ai-llm-api-examples/-/issues). If there is no issue yet, create one.
......@@ -21,4 +21,4 @@ We are very thankful for the examples you provide! We are a small team who canno
Show your appreciation to those who have contributed to the project.
## License
Please see the [LICENSE](LICENSE)
Please see the [LICENSE](LICENSE).
# Base functionality
This is a basic example that 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. Create a file `my_key` and put your API key in the file. For security reasons API Key is necessary. You have to obtain the API Key from the llm.scads.ai team. You can find instructions on https://llm.scads.ai/ .
2. Then simply start the script `main.py` from your bash shell:
```bash
source myenv/bin/activate
./main.py
```
3. Read the joke.
#!/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 = ""
try:
with open("my_key") as keyfile:
my_api_key = keyfile.readline().strip()
except FileNotFoundError:
print("Error: The file 'my_key' was not found. Please make sure the file exists and contains your API key.")
exit(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)
openai
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment