From b2afdda4e83687effd9a13cd99f0e3e21a578576 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 7 Nov 2023 07:35:04 -0800 Subject: [PATCH] Add more API examples --- docs/12 - OpenAI API.md | 73 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/docs/12 - OpenAI API.md b/docs/12 - OpenAI API.md index f5c683d0..d36bb77c 100644 --- a/docs/12 - OpenAI API.md +++ b/docs/12 - OpenAI API.md @@ -128,7 +128,7 @@ headers = { } history = [] - + while True: user_message = input("> ") history.append({"role": "user", "content": user_message}) @@ -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