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

Merge branch 'summarize-example' into 'main'

Add Summarize example

See merge request !2
parents b642af65 1b8977d6
No related branches found
No related tags found
1 merge request!2Add Summarize example
# ScaDS.AI LLM API Examples
This repository contains code snippets to get you started with our API. Before you start with any application example, please go to https://llm.scads.ai and request an API key.
This repository contains code snippets to get you started with our Large Language Model (LLM) API. Before you start with any application example, please go to https://llm.scads.ai and request an API key.
## Installation
Each example comes with its own installation instructions, because there are examples with several languages and environments which makes this necessary.
......@@ -8,6 +8,10 @@ Each example comes with its own installation instructions, because there are exa
## Usage
A base example is available in directory [base](base).
Further examples:
- [summarize](summarize) shows how to use a LLM to summarize a (plain) text file
## 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.
......
# Summarize a text file
This example intends to show you how to:
- Summarize a plain text file
- Adjust the temperature to reduce the models creativity
- Set the maximum length of a response
## 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 start the script `main.py` from your bash shell and provide a plain text file:
```bash
source myenv/bin/activate
./summarize.py README.md
```
openai
#!/usr/bin/env python
# Find instructions how to install dependencies and how to run this script in README.md
import sys
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)
# Find model with "llama" in name
for model in client.models.list().data:
model_name = model.id
if "llama" in model_name:
break
if len(sys.argv) > 1:
textfilename = sys.argv[1]
else:
print("No argument given, using README.md...")
textfilename = "README.md"
lines = []
with open(textfilename) as textfile:
lines = textfile.readlines()
filecontents = "\n".join(lines)
template = """### Instruction:
Summarize the following text:
### Input:
"""
request = template + filecontents
# Use model
response = client.chat.completions.create(
messages=[
{"role":"user","content":request}
],
model=model_name,
temperature = 0.1, # avoid creativity
max_tokens = 2048 # longer answer
)
# Print the summary
print("""
Summary:
""")
summary = response.choices[0].message.content
print(summary)
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