"def reply(prompt): try: response = openAIclient.chat.completions.create(…
- User
"def reply(prompt): try: response = openAI_client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": "You are a helpful assistant that answers questions based on the given context. " "In no case should you stray away from the information in context." "You may suggest correction if you find any contextual data is factually wrong." }, {"role": "user", "content": f"{prompt}"}, ] )
codereturn response.choices[0].message.content.strip() except openai.error.OpenAIError as e: return f"An error occurred: {e}"MAIN FUNCTIONS 👇
def query(query): device_id = query.get('device_id') if not device_id: return {"error": "Device ID is required"}, 400
codeindex = get_index(device_id) query_text = query.get('query') if not query_text: return {"error": "Query text is required"}, 400 contexts = retreive_data(query_text, index) prompt = ( "Context: \n" + "\n".join(contexts) + f"\nQuestion: {query_text}" ) answer = reply(prompt) return {"answer": answer}, 200query_data = { "device_id": "pc1-01", "query": "How much did Google pay to buy Deepmind?" }"
Output: ({'answer': 'The provided context does not contain information about the amount Google paid to acquire DeepMind. However, it is known that Google acquired DeepMind for approximately $500 million in 2014.'}, 200)
Here, the answer seems to be fine but I do not want the output to say anything like "The provided context does not contain information about... ", just simply answer the question from your knowledge if it is not available in the provided context.
- Assistant
To achieve the desired behavior where the assistant provides a complete answer without mentioning the insufficiency of the context, you can modify the system prompt to allow the assistant to use its own knowledge when necessary. Here's how you can adjust your code:
Modify the system message in your
replyfunction:python