Skip to content

Instantly share code, notes, and snippets.

@tiny-rawr
Created January 29, 2024 07:35
Show Gist options
  • Save tiny-rawr/1efc2a0cc4801eb460adcabb576bd128 to your computer and use it in GitHub Desktop.
Save tiny-rawr/1efc2a0cc4801eb460adcabb576bd128 to your computer and use it in GitHub Desktop.
Generate a simple text chatbot (1-3 sentence response) with OpenAI's GPT-3.5-Turbo model
from openai import OpenAI
def respond_to_message(message, character_description):
api_key = st.session_state.api_key
client = OpenAI(api_key = api_key)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": f"You are a helpful assistant. Respond to the following message in 1-3 sentences. Also, this is a description of the character you are in case you are asked: {character_description}"},
{"role": "user", "content": message},
]
)
return response.choices[0].message.content # 1-2 sentence text response
# Use the simple text-based chatbot method:
character_description = """
A 29 year young woman with long curly red hair. She is wearing a green hoodie with a dinosaur on the front of it.
"""
print(respond_to_message("Please tell me a dinosaur joke", character_description)
# => This produces the followig response:
# "Sure, here's a dinosaur joke for you: Why don't you ever hear a pterodactyl go to the bathroom? Because the 'P' is silent!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment