Add more API examples

This commit is contained in:
oobabooga 2023-11-07 07:35:04 -08:00
parent fd893baba1
commit b2afdda4e8

View File

@ -144,6 +144,77 @@ while True:
print(assistant_message)
```
#### Python chat example with streaming
Start the script with `python -u` to see the output in real time.
```python
import requests
import sseclient # pip install sseclient-py
import json
url = "http://127.0.0.1:5000/v1/chat/completions"
headers = {
"Content-Type": "application/json"
}
history = []
while True:
user_message = input("> ")
history.append({"role": "user", "content": user_message})
data = {
"mode": "instruct",
"stream": True,
"messages": history
}
stream_response = requests.post(url, headers=headers, json=data, verify=False, stream=True)
client = sseclient.SSEClient(stream_response)
for event in client.events():
payload = json.loads(event.data)
print(payload['choices'][0]['message']['content'], end='')
print()
```
### Python completions example with streaming
Start the script with `python -u` to see the output in real time.
```python
import json
import requests
import sseclient # pip install sseclient-py
url = "http://127.0.0.1:5000/v1/completions"
headers = {
"Content-Type": "application/json"
}
data = {
"prompt": "This is a cake recipe:\n\n1.",
"max_tokens": 200,
"temperature": 1,
"top_p": 0.9,
"seed": 10,
"stream": True,
}
stream_response = requests.post(url, headers=headers, json=data, verify=False, stream=True)
client = sseclient.SSEClient(stream_response)
print(data['prompt'], end='')
for event in client.events():
payload = json.loads(event.data)
print(payload['choices'][0]['text'], end='')
print()
```
### Client Application Setup