Truncate prompt to not exceed OpenAI prompt limit

Truncate prompt containing the top retrieved entry to 500 words to
avoid triggering the max_token limit error
This commit is contained in:
Debanjum Singh Solanky 2023-01-09 00:51:46 -03:00
parent 237123d18c
commit d36da46f7b

View file

@ -174,6 +174,7 @@ def converse(text, model, conversation_history=None, api_key=None, temperature=0
Converse with user using OpenAI's GPT
"""
# Initialize Variables
max_words = 500
openai.api_key = api_key or os.getenv("OPENAI_API_KEY")
conversation_primer = f'''
@ -184,6 +185,7 @@ AI: Hi, I am an AI conversational companion created by OpenAI. How can I help yo
# Setup Prompt with Primer or Conversation History
prompt = message_to_prompt(text, conversation_history or conversation_primer)
prompt = ' '.join(prompt.split()[:max_words])
# Get Response from GPT
response = openai.Completion.create(