如何从上面的completion中一个一个读取返回值
时间: 2024-02-13 17:04:54 浏览: 30
要从上面的completion中一个一个读取返回值,您需要使用异步编程的技术。在使用异步编程时,您可以使用回调函数来处理异步操作的结果。当异步操作完成时,结果将传递给回调函数,您可以在回调函数中访问它们。
在使用ChitGPT进行对话时,您可以编写一个回调函数来处理ChitGPT返回的结果。例如,您可以使用以下代码来使用ChitGPT进行对话,并将每个回复打印到控制台:
```
from ChitGPT import ChitGPT
gpt = ChitGPT()
def callback(response):
print(response)
gpt.chat("Hello!", callback)
```
在这个例子中,我们定义了一个名为`callback`的函数来处理ChitGPT返回的结果。当我们调用`gpt.chat`方法时,我们将`callback`函数作为参数传递给它。每次ChitGPT返回一个回复时,它将调用`callback`函数并将回复作为参数传递给它。在这个例子中,`callback`函数只是简单地将回复打印到控制台,但您可以根据需要修改它来执行其他操作。
相关问题
上面的代码单独用一个方法做
可以将上面的代码封装成一个方法,方便调用和复用。以下是一个示例方法:
```python
import openai
import json
def generate_response(prompt):
# Set up OpenAI API credentials
openai.api_key = "YOUR_API_KEY"
# Define the prompt
prompt_text = f"Prompt: {prompt}\nResponse:"
# Generate the completions
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt_text,
max_tokens=60,
n=1,
stop=None,
temperature=0.7,
)
# Extract the generated text from the response
generated_text = response.choices[0].text.strip()
# Return the generated text as a string
return generated_text
```
这个方法接受一个字符串作为输入,并返回一个字符串作为输出,表示 AI 对该输入的回应。要使用这个方法,只需将所需的输入作为参数传递给 `generate_response` 方法即可,例如:
```python
prompt = "What is the meaning of life?"
response = generate_response(prompt)
print(response)
```
输出:
```
To find meaning in life, one must look within themselves and what they value most. It is different for everyone, but ultimately it is about finding purpose and fulfillment in one's existence.
```
将上面程序合并为一个程序文件
以下是将两个程序合并为一个程序文件的代码:
```python
import openai
import requests
import json
# OpenAI API credentials
openai.api_key = "YOUR_API_KEY"
# CSDN API credentials
csdn_app_key = "YOUR_APP_KEY"
csdn_app_secret = "YOUR_APP_SECRET"
csdn_username = "YOUR_USERNAME"
csdn_password = "YOUR_PASSWORD"
# Authenticate CSDN API
response = requests.post(
url="https://passport.csdn.net/v1/api/authorize",
params={
"client_id": csdn_app_key,
"response_type": "code",
"state": "random_string",
"redirect_uri": "http://localhost",
"display": "default"
},
headers={
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0"
},
auth=(csdn_username, csdn_password)
)
auth_code = json.loads(response.text)['data']['auth_code']
# Use auth code to get access token
response = requests.post(
url="https://gitee.com/oauth/token",
params={
"grant_type": "authorization_code",
"code": auth_code,
"client_id": csdn_app_key,
"redirect_uri": "http://localhost",
"client_secret": csdn_app_secret
},
headers={
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0"
}
)
access_token = json.loads(response.text)['access_token']
# Chat with OpenAI GPT-3
def chat_with_openai(prompt):
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=150,
n=1,
stop=None,
temperature=0.5
)
message = response.choices[0].text
return message.strip()
# Chat with CSDN ChitGPT
def chat_with_csdn(prompt):
response = requests.post(
url="https://aiapi.csdn.net/sdk/chit_chat",
headers={
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0",
"Authorization": f"Bearer {access_token}"
},
json={
"msg": prompt
}
)
message = json.loads(response.text)['data']['reply']
return message.strip()
# Main loop
while True:
user_input = input("You: ")
if user_input.lower() == "bye":
print("Bot: Bye!")
break
openai_response = chat_with_openai(user_input)
print("OpenAI: " + openai_response)
csdn_response = chat_with_csdn(user_input)
print("CSDN: " + csdn_response)
```
请注意,您需要将 `YOUR_API_KEY`,`YOUR_APP_KEY`,`YOUR_APP_SECRET`,`YOUR_USERNAME` 和 `YOUR_PASSWORD` 替换为您自己的 API 密钥和凭据。