Skip to content

Commit 670d928

Browse files
committed
Fixed WA's Inf. Loop msgs. Validated Claude's msg_hist
Main changes: fix: Modify main_whatsapp to handle incoming messages with background tasks for better performance feat: Implement message splitting functionality in WhatsAppPresenter to handle long messages fix: Added validation check to AnsariClaude to ensure that a "tool_use" block is followed by only 1 "tool_result" block (in self.message_history) Extra details: fix: Added \n\n in AnsariClaude's response to separate between the text before tool's output and the tool's output refactor: Remove unused `stream` option feat: Enhance logging capabilities in ansari_logger to support file logging in DEV_MODE docs: Add example JSON structures for API responses in documentation feat: Update .gitignore to include logs directory fix: Update Python version in Jupyter notebook for API response structure
1 parent 7230b7d commit 670d928

11 files changed

+613
-81
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ docs/recordings/*
1010
etc/
1111
example_projects/
1212
lib/
13+
logs/
1314
share/
1415
src/ansari_backend.egg-info/*
1516
tmp/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[ // This is a dummy example, so don't focus on the content of the messages
2+
{
3+
"role": "user",
4+
"content": "Can you search for information about machine learning?"
5+
},
6+
{
7+
"role": "assistant",
8+
"content": [
9+
{
10+
"type": "text",
11+
"text": "I'll search for information about machine learning for you."
12+
},
13+
{
14+
"type": "tool_use",
15+
"id": "tool-123456",
16+
"name": "search_database",
17+
"input": {
18+
"query": "machine learning",
19+
"limit": 3
20+
}
21+
}
22+
]
23+
},
24+
{
25+
"role": "user",
26+
"content": [
27+
{
28+
"type": "tool_result",
29+
"tool_use_id": "tool-123456",
30+
"content": "{\"results\": [{\"title\": \"Introduction to ML\", \"url\": \"https://example.com/ml1\"}, {\"title\": \"Deep Learning Fundamentals\", \"url\": \"https://example.com/ml2\"}]}"
31+
}
32+
]
33+
},
34+
{
35+
"role": "assistant",
36+
"content": [
37+
{
38+
"type": "text",
39+
"text": "Based on my search, here are some resources about machine learning:\n\n1. Introduction to ML - This resource covers the basics of machine learning algorithms and techniques.\n2. Deep Learning Fundamentals - This focuses on neural networks and deep learning approaches."
40+
}
41+
]
42+
},
43+
{
44+
"role": "user",
45+
"content": "Can you explain neural networks in more detail?"
46+
},
47+
{
48+
"role": "assistant",
49+
"content": [
50+
{
51+
"type": "text",
52+
"text": "Neural networks are computational models inspired by the human brain. They consist of layers of interconnected nodes or \"neurons\" that process information.\n\nThe basic structure includes:\n\n1. Input layer: Receives initial data\n2. Hidden layers: Perform computations on inputs\n3. Output layer: Produces the final result\n\nEach connection between neurons has a weight that gets adjusted during training[1]."
53+
}
54+
]
55+
}
56+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"type": "content_block_start | content_block_delta | content_block_stop | message_delta | message_stop",
3+
"content_block": {
4+
"type": "text | tool_use",
5+
"id": "tool-123456",
6+
"name": "search_database"
7+
},
8+
"delta": {
9+
"text": "Here's information about your query...",
10+
"partial_json": "{ \"query\": \"ma",
11+
"type": "citations_delta",
12+
"citation": {
13+
"start": 23,
14+
"end": 45,
15+
"number": 1,
16+
"text": "according to source X..."
17+
},
18+
"stop_reason": "end_turn | tool_use"
19+
}
20+
}

docs/structure_of_api_responses/openai_api_structure_of_chat_completion_chunk_object.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
},
111111
"language_info": {
112112
"name": "python",
113-
"version": "3.12.5"
113+
"version": "3.13.2"
114114
}
115115
},
116116
"nbformat": 4,

src/ansari/agents/ansari.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def process_input(self, user_input: str):
9595
if m:
9696
yield m
9797

98-
def replace_message_history(self, message_history: list[dict], use_tool=True, stream=True):
98+
def replace_message_history(self, message_history: list[dict], use_tool=True):
9999
"""
100100
Replaces the current message history (stored in Ansari) with the given message history,
101101
and then processes it to generate a response from Ansari.
@@ -106,23 +106,19 @@ def replace_message_history(self, message_history: list[dict], use_tool=True, st
106106
{"role": "system", "content": self.sys_msg},
107107
] + message_history
108108

109-
# Return/Yield Ansari's response to the user
110-
# TODO(odyash) later (good_first_issue): `stream == False` is not implemented yet; so it has to stay `True`
111-
for m in self.process_message_history(use_tool, stream=stream):
109+
# Yield Ansari's response to the user
110+
for m in self.process_message_history(use_tool):
112111
if m:
113112
yield m
114113

115114
def get_completion(self, **kwargs):
116115
return litellm.completion(**kwargs)
117116

118-
def process_message_history(self, use_tool=True, stream=True):
119-
"""
120-
TODO(odyash) later (good_first_issue): `stream == False` is not implemented yet; so it has to stay `True`
121-
"""
117+
def process_message_history(self, use_tool=True):
122118
common_params = {
123119
"model": self.model,
124120
"messages": self.message_history,
125-
"stream": stream,
121+
"stream": True,
126122
"stream_options": {"include_usage": True},
127123
"timeout": 30.0,
128124
"temperature": 0.0,
@@ -225,14 +221,11 @@ def process_message_history(self, use_tool=True, stream=True):
225221
else:
226222
raise Exception("Invalid response mode: " + response_mode)
227223

228-
def process_one_round(self, use_tool=True, stream=True):
229-
"""
230-
TODO(odyash) later (good_first_issue): `stream == False` is not implemented yet; so it has to stay `True`
231-
"""
224+
def process_one_round(self, use_tool=True):
232225
common_params = {
233226
"model": self.model,
234227
"messages": self.message_history,
235-
"stream": stream,
228+
"stream": True,
236229
"stream_options": {"include_usage": True},
237230
"timeout": 30.0,
238231
"temperature": 0.0,

0 commit comments

Comments
 (0)