Reputation: 21
I'm new in python and i want a little hand into this code. I'm developing a smart chatbot using the openai API and using it in what's app. I have this piece of my code that is responsible for the chatgpt response in my code. At the moment, this code is on model = "text-davinci-003" and i want to turn it into "gpt-3.5-turbo". Is any good soul interested in helping me?
Obs.: "msg" is what we ask to chatgpt
on whatsapp
The piece of my code:
msg = todas_as_msg_texto[-1]
print(msg) # -> Mensagem que o cliente manda (no caso eu)
cliente = 'msg do cliente: '
texto2 = 'Responda a mensagem do cliente com base no próximo texto: '
questao = cliente + msg + texto2 + texto
# #### PROCESSA A MENSAGEM NA API DO CHAT GPT ####
openai.api_key= apiopenai.strip()
response=openai.Completion.create(
model="text-davinci-003",
prompt=questao,
temperature=0.1,
max_tokens=270,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
)
resposta=response['choices'][0]['text']
print(resposta)
time.sleep(1)
Upvotes: 1
Views: 2508
Reputation: 1777
To update your code to gpt-3.5-turbo
, there are four areas you need to modify:
openai.ChatCompletion.create
instead of openai.Completion.create
model='gpt-3.5-turbo'
messages=
to an array as shown belowrepsonse
to your resposta
variable so that you are reading from the messages
keyThis tested example takes into account those changes:
response=openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": questao }],
temperature=0.1,
max_tokens=270,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
)
resposta=response['choices'][0]['message']['content']
Additionally, since more than one choice can be returned from the model, instead of only looking at [0]
you may be interested in iterating over them to see what you're getting, something like:
for choice in response.choices:
outputText = choice.message.content
print(outputText)
print("------")
print("\n")
Note that you don't need to do that if you are calling openai.ChatCompletion.create
with 'n=1'
Additionally, your example is setting both temperature
and top_p
, however the docs suggest to only set one of those variables.
Upvotes: 1
Reputation: 71
You can try this
import requests
import json
# Create HTTP client and request objects
httpClient1 = requests.Session()
url = "https://api.openai.com/v1/chat/completions"
headers = {"Authorization": "Bearer " + OpenaiApiKey}
prompt = "Hello, how are you?"
request1 = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": prompt}]
}
# Send the request and wait for the response
response1 = httpClient1.post(url, headers=headers, json=request1)
responseContent1 = response1.content
# Deserialize the JSON response and extract the generated text
responseObject1 = json.loads(responseContent1.decode('utf-8'))
results = responseObject1["choices"][0]["text"]
Upvotes: -1