Skip to content

Commit 96db02b

Browse files
committed
Remove chat_completion tool from shared and app modules
The chat_completion tool and its related code have been removed from shared/tools.py and are no longer re-exported in the yelp-navigator app tools modules (v1, v2, v3). This simplifies the toolset and eliminates unused or deprecated functionality.
1 parent df5d982 commit 96db02b

File tree

5 files changed

+3
-163
lines changed

5 files changed

+3
-163
lines changed

epilogue/context-engineering/shared/chat_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
from typing import Dict, Any, Optional, Tuple
77

88

9-
def handle_general_chat(state: Dict[str, Any], track_errors: bool = False) -> Tuple[str, Optional[Dict[str, Any]]]:
9+
def handle_general_chat(track_errors: bool = False) -> Tuple[str, Optional[Dict[str, Any]]]:
1010
"""
1111
Handle non-business chat queries with a welcoming message that redirects to business searches.
1212
1313
It always returns a friendly welcome message that explains what the chatbot
1414
is designed for (business search and analysis) and prompts the user to ask about businesses.
1515
1616
Args:
17-
state: Current agent state (not used, but kept for consistency)
1817
track_errors: If True, returns error tracking dict; if False, returns None
1918
2019
Returns:

epilogue/context-engineering/shared/tools.py

Lines changed: 1 addition & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,10 @@
11
"""Shared tools for interacting with Hayhooks API endpoints."""
22
import requests
3-
from typing import Dict, Any, List
3+
from typing import Dict, Any
44
from langchain_core.tools import tool
5-
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
6-
from .config import get_llm
75

86
BASE_URL = "http://localhost:1416"
97

10-
MODEL = "gpt-oss:20b" #"gpt-4o-mini" #change to your preferred model
11-
12-
# OpenAI Chat Completion Tool
13-
@tool
14-
def chat_completion(messages: List[Dict[str, str]], model: str = MODEL, stream: bool = False, base_url: str = BASE_URL) -> Dict[str, Any]:
15-
"""Send a general chat completion request using OpenAI's chat completion API.
16-
17-
Args:
18-
messages: List of message dicts, e.g. [{"role": "user", "content": "Hello!"}]
19-
model: Model name to use (please review shared/config.py for supported models)
20-
stream: Whether to stream the response (default: False)
21-
base_url: Base URL for the API endpoint (deprecated, kept for compatibility)
22-
23-
Returns:
24-
Dictionary containing the chat completion response in OpenAI format:
25-
{
26-
"success": True/False,
27-
"response": {
28-
"id": "string",
29-
"object": "chat.completion",
30-
"created": int,
31-
"model": "string",
32-
"choices": [
33-
{
34-
"index": 0,
35-
"message": {
36-
"role": "assistant",
37-
"content": "string"
38-
},
39-
"finish_reason": "stop"
40-
}
41-
]
42-
},
43-
"error": "error message" (only if success is False)
44-
}
45-
"""
46-
max_attempts = 3
47-
last_error = None
48-
49-
for attempt in range(max_attempts):
50-
try:
51-
# Get the LLM instance with error handling
52-
try:
53-
llm = get_llm(model=model)
54-
except Exception as llm_error:
55-
if attempt == max_attempts - 1:
56-
return {
57-
"success": False,
58-
"error": f"Failed to initialize model '{model}': {str(llm_error)}. Please check your API keys and model configuration."
59-
}
60-
# Wait before retry (exponential backoff)
61-
import time
62-
time.sleep(2 ** attempt)
63-
continue
64-
65-
# Add system instruction to always end with a summary
66-
system_instruction = SystemMessage(content=(
67-
"You are a helpful assistant for a Yelp business search and analysis system. "
68-
"After answering the user's question, ALWAYS end your response with a brief summary that includes:\n"
69-
"1. What this agentic system does (searches for businesses, analyzes reviews, and provides recommendations)\n"
70-
"2. What information it needs from users (location, business type/category, and specific preferences or requirements)"
71-
))
72-
73-
# Convert message dicts to LangChain message objects
74-
langchain_messages = [system_instruction]
75-
for msg in messages:
76-
role = msg.get("role", "user")
77-
content = msg.get("content", "")
78-
79-
if role == "system":
80-
langchain_messages.append(SystemMessage(content=content))
81-
elif role == "assistant":
82-
langchain_messages.append(AIMessage(content=content))
83-
else: # user or any other role
84-
langchain_messages.append(HumanMessage(content=content))
85-
86-
# Invoke the LLM with timeout protection
87-
try:
88-
response = llm.invoke(langchain_messages)
89-
except Exception as invoke_error:
90-
last_error = invoke_error
91-
if "rate limit" in str(invoke_error).lower() or "quota" in str(invoke_error).lower():
92-
# Rate limit error - wait longer
93-
if attempt < max_attempts - 1:
94-
import time
95-
wait_time = min(30, 5 * (2 ** attempt))
96-
print(f"Rate limit hit, waiting {wait_time}s before retry...")
97-
time.sleep(wait_time)
98-
continue
99-
elif "timeout" in str(invoke_error).lower():
100-
# Timeout error - retry with shorter timeout
101-
if attempt < max_attempts - 1:
102-
import time
103-
time.sleep(2)
104-
continue
105-
raise # Re-raise if not rate limit or last attempt
106-
107-
# Format response in OpenAI-compatible format
108-
return {
109-
"success": True,
110-
"response": {
111-
"id": getattr(response, "id", "chatcmpl-" + str(hash(response.content))),
112-
"object": "chat.completion",
113-
"created": int(getattr(response, "response_metadata", {}).get("created", 0)),
114-
"model": model,
115-
"choices": [
116-
{
117-
"index": 0,
118-
"message": {
119-
"role": "assistant",
120-
"content": response.content
121-
},
122-
"finish_reason": "stop"
123-
}
124-
]
125-
},
126-
"metadata": {
127-
"attempts": attempt + 1,
128-
"model_used": model
129-
}
130-
}
131-
132-
except Exception as e:
133-
last_error = e
134-
if attempt == max_attempts - 1:
135-
error_type = type(e).__name__
136-
error_details = str(e)
137-
138-
# Provide helpful error messages based on error type
139-
if "api_key" in error_details.lower() or "authentication" in error_details.lower():
140-
error_msg = f"Authentication failed for model '{model}': Check your API keys in .env file"
141-
elif "not found" in error_details.lower() or "does not exist" in error_details.lower():
142-
error_msg = f"Model '{model}' not found. Available models depend on your provider."
143-
elif "connection" in error_details.lower() or "network" in error_details.lower():
144-
error_msg = f"Network error connecting to {model}: Check your internet connection"
145-
else:
146-
error_msg = f"Error in chat completion ({error_type}): {error_details}"
147-
148-
return {
149-
"success": False,
150-
"error": error_msg,
151-
"error_type": error_type,
152-
"attempts": max_attempts
153-
}
154-
155-
# Should not reach here, but just in case
156-
return {
157-
"success": False,
158-
"error": f"Failed after {max_attempts} attempts: {str(last_error)}",
159-
"attempts": max_attempts
160-
}
1618

1629
@tool
16310
def search_businesses(query: str) -> Dict[str, Any]:

epilogue/context-engineering/yelp-navigator-v1/app/tools.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
"""Re-export shared tools for backwards compatibility."""
22
from shared.tools import (
3-
chat_completion,
43
search_businesses,
54
get_business_details,
65
analyze_reviews_sentiment,
76
BASE_URL
87
)
98

109
__all__ = [
11-
'chat_completion',
1210
'search_businesses',
1311
'get_business_details',
1412
'analyze_reviews_sentiment',

epilogue/context-engineering/yelp-navigator-v2/app/tools.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
"""Re-export shared tools for backwards compatibility."""
22
from shared.tools import (
3-
chat_completion,
43
search_businesses,
54
get_business_details,
65
analyze_reviews_sentiment,
76
BASE_URL
87
)
98

109
__all__ = [
11-
'chat_completion',
1210
'search_businesses',
1311
'get_business_details',
1412
'analyze_reviews_sentiment',

epilogue/context-engineering/yelp-navigator-v3/app/tools.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
"""V3 Tools - re-exports shared tools for backwards compatibility."""
1+
"""Re-export shared tools for backwards compatibility."""
22
from shared.tools import (
3-
chat_completion,
43
search_businesses,
54
get_business_details,
65
analyze_reviews_sentiment,
76
BASE_URL
87
)
98

109
__all__ = [
11-
'chat_completion',
1210
'search_businesses',
1311
'get_business_details',
1412
'analyze_reviews_sentiment',

0 commit comments

Comments
 (0)