Assigns a reward of -1 to negative past experiences.
bad_queries_responses = []
if isinstance(bad_client_memory, dict):
for memory_key, memory_data in bad_client_memory.items():
if isinstance(memory_data, dict):
memory_query_text = memory_data.get('query_text', '')
memory_response_text = memory_data.get('response_text', '')
if memory_query_text and memory_response_text:
bad_queries_responses.append((memory_query_text, memory_response_text, -1))
3. Fetching Good Memories
Retrieves good memories using MemoryLake with a different memory_type.
Extracts positive query-response pairs with a reward score of +1.
good_queries_responses = []
if isinstance(good_client_memory, dict):
for memory_key, memory_data in good_client_memory.items():
if isinstance(memory_data, dict):
memory_query_text = memory_data.get('query_text', '')
memory_response_text = memory_data.get('response_text', '')
if memory_query_text and memory_response_text:
good_queries_responses.append((memory_query_text, memory_response_text, 1))
4. Refining the Prompt
Constructs a chat payload with:
System message defining the AI’s goal.
User message with:
The new query.
Previously bad responses.
Previously good responses.
chat_payload = {
"messages": [
{
"role": "system",
"content": (
f"You are an advanced assistant designed to refine prompts based on an agent's past experiences. "
f"Your task is to improve the given prompt by considering both good and bad memories of the agent. "
f"Use the reward values to guide the refinement process and optimize the prompt for high-quality responses."
)
},
{
"role": "user",
"content": (
f"Here is a prompt that needs refinement: {query_text}\n\n"
f"### Past Experiences with Rewards\n\n"
f"### Bad Memories (Reward: -1)\n"
f"{bad_queries_responses_text}\n\n"
f"### Good Memories (Reward: +1)\n"
f"{good_queries_responses_text}\n\n"
"Refine the prompt to maximize response quality while avoiding past mistakes."
)
}
]
}
5. Generating a Refined Response
Calls chat_complete() to generate a refined response.
If an error occurs, it returns an appropriate error message.
try:
chat_response = self.model.chat_complete(chat_payload)
except Exception as e:
return {
"query_text": query_text,
"response_text": f"Error with chat completion: {str(e)}"
}
Edit{
"query_text": "How can I improve AI model accuracy?"
}
Response
Edit{
"query_text": "How can I improve AI model accuracy?",
"response_text": "To enhance AI model accuracy, consider data augmentation, hyperparameter tuning, and leveraging transfer learning techniques."
}