Skip to content

Instantly share code, notes, and snippets.

@sravantit25
Created June 7, 2023 16:45
Show Gist options
  • Select an option

  • Save sravantit25/4f2d516f718dde61dfe8e7c171be2a43 to your computer and use it in GitHub Desktop.

Select an option

Save sravantit25/4f2d516f718dde61dfe8e7c171be2a43 to your computer and use it in GitHub Desktop.
"""
This script uses ChatGPT, a conversational language model, to generate a reply based on a user message.
It takes a user message as input and utilizes ChatGPT to generate a response.
Uses gpt-3.5-turbo model. An OpenAI API key is required.
"""
import os
import json
import openai
model_engine = "gpt-3.5-turbo"
openai.api_key = os.getenv("API_KEY")
def get_response(input_message, system_prompt, max_tokens=None):
"Return the ChatGPT response"
response = openai.ChatCompletion.create(
model=model_engine,
messages=[
{"role": "user", "content": input_message},
{"role": "system", "content": system_prompt},
],
max_tokens=max_tokens,
)
reply = f"{response['choices'][0]['message']['content']}"
return reply
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment