Formatting (#2750)
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
"import anthropic\n",
|
||||
"\n",
|
||||
"# Set up environment variables\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = \"your_openai_api_key\" # needed for embedding model\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = \"your_openai_api_key\" # needed for embedding model\n",
|
||||
"os.environ[\"ANTHROPIC_API_KEY\"] = \"your_anthropic_api_key\""
|
||||
]
|
||||
},
|
||||
@@ -33,7 +33,7 @@
|
||||
" \"model\": \"claude-3-5-sonnet-latest\",\n",
|
||||
" \"temperature\": 0.1,\n",
|
||||
" \"max_tokens\": 2000,\n",
|
||||
" }\n",
|
||||
" },\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
" self.client = anthropic.Client(api_key=os.environ[\"ANTHROPIC_API_KEY\"])\n",
|
||||
@@ -50,11 +50,7 @@
|
||||
" - Keep track of open issues and follow-ups\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" def store_customer_interaction(self,\n",
|
||||
" user_id: str,\n",
|
||||
" message: str,\n",
|
||||
" response: str,\n",
|
||||
" metadata: Dict = None):\n",
|
||||
" def store_customer_interaction(self, user_id: str, message: str, response: str, metadata: Dict = None):\n",
|
||||
" \"\"\"Store customer interaction in memory.\"\"\"\n",
|
||||
" if metadata is None:\n",
|
||||
" metadata = {}\n",
|
||||
@@ -63,24 +59,17 @@
|
||||
" metadata[\"timestamp\"] = datetime.now().isoformat()\n",
|
||||
"\n",
|
||||
" # Format conversation for storage\n",
|
||||
" conversation = [\n",
|
||||
" {\"role\": \"user\", \"content\": message},\n",
|
||||
" {\"role\": \"assistant\", \"content\": response}\n",
|
||||
" ]\n",
|
||||
" conversation = [{\"role\": \"user\", \"content\": message}, {\"role\": \"assistant\", \"content\": response}]\n",
|
||||
"\n",
|
||||
" # Store in Mem0\n",
|
||||
" self.memory.add(\n",
|
||||
" conversation,\n",
|
||||
" user_id=user_id,\n",
|
||||
" metadata=metadata\n",
|
||||
" )\n",
|
||||
" self.memory.add(conversation, user_id=user_id, metadata=metadata)\n",
|
||||
"\n",
|
||||
" def get_relevant_history(self, user_id: str, query: str) -> List[Dict]:\n",
|
||||
" \"\"\"Retrieve relevant past interactions.\"\"\"\n",
|
||||
" return self.memory.search(\n",
|
||||
" query=query,\n",
|
||||
" user_id=user_id,\n",
|
||||
" limit=5 # Adjust based on needs\n",
|
||||
" limit=5, # Adjust based on needs\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" def handle_customer_query(self, user_id: str, query: str) -> str:\n",
|
||||
@@ -112,15 +101,12 @@
|
||||
" model=\"claude-3-5-sonnet-latest\",\n",
|
||||
" messages=[{\"role\": \"user\", \"content\": prompt}],\n",
|
||||
" max_tokens=2000,\n",
|
||||
" temperature=0.1\n",
|
||||
" temperature=0.1,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # Store interaction\n",
|
||||
" self.store_customer_interaction(\n",
|
||||
" user_id=user_id,\n",
|
||||
" message=query,\n",
|
||||
" response=response,\n",
|
||||
" metadata={\"type\": \"support_query\"}\n",
|
||||
" user_id=user_id, message=query, response=response, metadata={\"type\": \"support_query\"}\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" return response.content[0].text"
|
||||
@@ -203,12 +189,12 @@
|
||||
" # Get user input\n",
|
||||
" query = input()\n",
|
||||
" print(\"Customer:\", query)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" # Check if user wants to exit\n",
|
||||
" if query.lower() == 'exit':\n",
|
||||
" if query.lower() == \"exit\":\n",
|
||||
" print(\"Thank you for using our support service. Goodbye!\")\n",
|
||||
" break\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" # Handle the query and print the response\n",
|
||||
" response = chatbot.handle_customer_query(user_id, query)\n",
|
||||
" print(\"Support:\", response, \"\\n\\n\")"
|
||||
|
||||
@@ -25,7 +25,8 @@
|
||||
"source": [
|
||||
"# Set up ENV Vars\n",
|
||||
"import os\n",
|
||||
"os.environ['OPENAI_API_KEY'] = \"sk-xxx\"\n"
|
||||
"\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = \"sk-xxx\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -133,11 +134,9 @@
|
||||
"assistant_id = os.environ.get(\"ASSISTANT_ID\", None)\n",
|
||||
"\n",
|
||||
"# LLM Configuration\n",
|
||||
"CACHE_SEED = 42 # choose your poison\n",
|
||||
"CACHE_SEED = 42 # choose your poison\n",
|
||||
"llm_config = {\n",
|
||||
" \"config_list\": [\n",
|
||||
" {\"model\": \"gpt-4o\", \"api_key\": os.environ[\"OPENAI_API_KEY\"]}\n",
|
||||
" ],\n",
|
||||
" \"config_list\": [{\"model\": \"gpt-4o\", \"api_key\": os.environ[\"OPENAI_API_KEY\"]}],\n",
|
||||
" \"cache_seed\": CACHE_SEED,\n",
|
||||
" \"timeout\": 120,\n",
|
||||
" \"temperature\": 0.0,\n",
|
||||
@@ -348,7 +347,7 @@
|
||||
"source": [
|
||||
"# Retrieve the memory\n",
|
||||
"relevant_memories = MEM0_MEMORY_CLIENT.search(user_query, user_id=USER_ID, limit=3)\n",
|
||||
"relevant_memories_text = '\\n'.join(mem['memory'] for mem in relevant_memories)\n",
|
||||
"relevant_memories_text = \"\\n\".join(mem[\"memory\"] for mem in relevant_memories)\n",
|
||||
"print(\"Relevant memories:\")\n",
|
||||
"print(relevant_memories_text)\n",
|
||||
"\n",
|
||||
@@ -389,8 +388,8 @@
|
||||
"# - Enables more context-aware and personalized agent responses.\n",
|
||||
"# - Bridges the gap between human input and AI processing in complex workflows.\n",
|
||||
"\n",
|
||||
"class Mem0ProxyCoderAgent(UserProxyAgent):\n",
|
||||
"\n",
|
||||
"class Mem0ProxyCoderAgent(UserProxyAgent):\n",
|
||||
" def __init__(self, *args, **kwargs):\n",
|
||||
" super().__init__(*args, **kwargs)\n",
|
||||
" self.memory = MEM0_MEMORY_CLIENT\n",
|
||||
@@ -399,15 +398,14 @@
|
||||
" def initiate_chat(self, assistant, message):\n",
|
||||
" # Retrieve memory for the agent\n",
|
||||
" agent_memories = self.memory.search(message, agent_id=self.agent_id, limit=3)\n",
|
||||
" agent_memories_txt = '\\n'.join(mem['memory'] for mem in agent_memories)\n",
|
||||
" agent_memories_txt = \"\\n\".join(mem[\"memory\"] for mem in agent_memories)\n",
|
||||
" prompt = f\"{message}\\n Coding Preferences: \\n{str(agent_memories_txt)}\"\n",
|
||||
" response = super().initiate_chat(assistant, message=prompt)\n",
|
||||
" # Add new memory after processing the message\n",
|
||||
" response_dist = response.__dict__ if not isinstance(response, dict) else response\n",
|
||||
" MEMORY_DATA = [{\"role\": \"user\", \"content\": message}, {\"role\": \"assistant\", \"content\": response_dist}]\n",
|
||||
" self.memory.add(MEMORY_DATA, agent_id=self.agent_id)\n",
|
||||
" return response\n",
|
||||
" "
|
||||
" return response"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -560,12 +558,12 @@
|
||||
"from cookbooks.helper.mem0_teachability import Mem0Teachability\n",
|
||||
"\n",
|
||||
"teachability = Mem0Teachability(\n",
|
||||
" verbosity=2, # for visibility of what's happening\n",
|
||||
" recall_threshold=0.5,\n",
|
||||
" reset_db=False, # Use True to force-reset the memo DB, and False to use an existing DB.\n",
|
||||
" agent_id=AGENT_ID,\n",
|
||||
" memory_client = MEM0_MEMORY_CLIENT,\n",
|
||||
" )\n",
|
||||
" verbosity=2, # for visibility of what's happening\n",
|
||||
" recall_threshold=0.5,\n",
|
||||
" reset_db=False, # Use True to force-reset the memo DB, and False to use an existing DB.\n",
|
||||
" agent_id=AGENT_ID,\n",
|
||||
" memory_client=MEM0_MEMORY_CLIENT,\n",
|
||||
")\n",
|
||||
"teachability.add_to_agent(user_proxy)"
|
||||
]
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user