From d36da46f7bbb2b88a4c7f25814ad80c00b463f07 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 9 Jan 2023 00:51:46 -0300 Subject: [PATCH] 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 --- src/processor/conversation/gpt.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/processor/conversation/gpt.py b/src/processor/conversation/gpt.py index 85d3e4ab..5f312b78 100644 --- a/src/processor/conversation/gpt.py +++ b/src/processor/conversation/gpt.py @@ -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(