File size: 599 Bytes
cc93c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import openai

openai.api_key = "sk-DQ1nFYzAVzGMznofdi0nig7MebfA9PWrTxCHlLIZIqc4X8xu"
openai.api_base = "https://api.chatanywhere.cn/v1"

def generator():
    messages = [{
            "role": "user",
            "content": "What is the meaning of life?",
    }]
    response = ""
    for chunk in openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=messages,
            temperature=0.9,
            stream=True,
    ):
        content = chunk["choices"][0].get("delta", {}).get("content")
        if content:
            response += content
        yield response