mirror of
https://github.com/oobabooga/text-generation-webui.git
synced 2024-11-26 01:30:20 +01:00
Add more API examples
This commit is contained in:
parent
fd893baba1
commit
b2afdda4e8
@ -144,6 +144,77 @@ while True:
|
|||||||
print(assistant_message)
|
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
|
### Client Application Setup
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user