improved performance with openai but not perfect

This commit is contained in:
Steve Androulakis
2025-02-13 08:53:28 -08:00
parent a8c62134fd
commit 0bc4a0e0aa
4 changed files with 30 additions and 19 deletions

View File

@@ -57,18 +57,20 @@ class ToolActivities:
# Create validation prompt
validation_prompt = f"""The user's prompt is: "{validation_input.prompt}"
Please validate if this prompt makes sense given the agent goal and conversation history.
If the prompt doesn't make sense toward the goal then validationResult should be true.
Only return false if the prompt is nonsensical given the goal, tools available, and conversation history.
If the prompt makes sense toward the goal then validationResult should be true.
If the prompt is wildly nonsensical or makes no sense toward the goal and current conversation history then validationResult should be false.
If the response is low content such as "yes" or "that's right" then the user is probably responding to a previous prompt.
Therefore examine it in the context of the conversation history to determine if it makes sense and return true if it makes sense.
Return ONLY a JSON object with the following structure:
"validationResult": true/false,
"validationFailedReason": "If validationResult is false, provide a clear explanation to the user
"validationFailedReason": "If validationResult is false, provide a clear explanation to the user in the response field
about why their request doesn't make sense in the context and what information they should provide instead.
validationFailedReason should contain JSON in the format
{{
"next": "question",
"response": "[your reason here and a response to get the user back on track with the agent goal]"
}}
If validationResult is true, return an empty dict {{}}"
If validationResult is true (the prompt makes sense), return an empty dict as its value {{}}"
"""
# Call the LLM with the validation prompt
@@ -172,8 +174,10 @@ class ToolActivities:
genai.configure(api_key=api_key)
model = genai.GenerativeModel(
"models/gemini-2.0-flash-exp",
system_instruction=input.context_instructions,
"models/gemini-1.5-flash",
system_instruction=input.context_instructions
+ ". The current date is "
+ datetime.now().strftime("%B %d, %Y"),
)
response = model.generate_content(input.prompt)
response_content = response.text
@@ -196,8 +200,9 @@ class ToolActivities:
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system=input.context_instructions + ". The current date is "
+ get_current_date_human_readable(),
system=input.context_instructions
+ ". The current date is "
+ get_current_date_human_readable(),
messages=[
{
"role": "user",

View File

@@ -23,17 +23,23 @@ def parse_datetime(datetime_str):
"%Y-%m-%dT%H:%M", # e.g. "2025-04-18T09:00"
"%Y-%m-%dT%H:%M:%S", # e.g. "2025-04-18T09:00:00"
"%Y-%m-%d %H:%M:%S", # e.g. "2025-04-18 09:00:00"
"%Y-%m-%d", # e.g. "2025-04-11"
]
for fmt in formats:
try:
parsed = time.strptime(datetime_str, fmt)
if fmt == "%Y-%m-%d":
# Default to 9am if no time provided
hour, minute = 9, 0
else:
hour, minute = parsed.tm_hour, parsed.tm_min
return (
parsed.tm_year,
parsed.tm_mon,
parsed.tm_mday,
parsed.tm_hour,
parsed.tm_min,
hour,
minute,
)
except ValueError:
continue

View File

@@ -16,9 +16,9 @@ goal_match_train_invoice = AgentGoal(
],
description="Help the user book trains to a premier league match. The user lives in London. Gather args for these tools in order: "
"1. SearchFixtures: Search for fixtures for a team in a given month"
"2. SearchTrains: Search for trains to visit somewhere before or after the match"
"2. SearchTrains: Search for trains to the city of the match and list them for the customer to choose from"
"3. BookTrain: Book the train tickets"
"4. CreateInvoice: Create a simple invoice for the cost of the flights and train tickets",
"4. CreateInvoice: Proactively offer to create a simple invoice for the cost of the flights and train tickets",
starter_prompt="Welcome me, give me a description of what you can do, then ask me for the details you need to do your job",
example_conversation_history="\n ".join(
[
@@ -28,13 +28,13 @@ goal_match_train_invoice = AgentGoal(
"agent: Great! Let's find a match for Wolverhampton Wanderers FC in May.",
"user_confirmed_tool_run: <user clicks confirm on SearchFixtures tool, passing the full team name as an input>",
'tool_result: results including {"homeTeam": "Wolverhampton Wanderers FC", "awayTeam": "Manchester United", "date": "2025-05-04"}',
"agent: Found a match! There's an away game against Manchester United on May 4 2025. Would you like to plan train travel from London for around this date?",
"agent: Found a match! <agent provides a human-readable list of match options including the location and date>",
"user_confirmed_tool_run: <user clicks confirm on SearchTrains tool>",
"tool_result: results including train dates and times, origin and depature stations",
"agent: Found some trains! The best option is leaving <origin> on <date> <time> and arriving in <destination> at <date> <time>. The return trip is leaving <origin> on <date> <time> and arriving in <destination> at <date> <time>. Would you like to book this train?",
"tool_result: <results including train dates and times, origin and depature stations>",
"agent: Found some trains! <agent provides a human-readable list of train options>",
"user_confirmed_tool_run: <user clicks confirm on BookTrain tool>",
'tool_result: results including {"status": "success"}',
"agent: Train tickets booked! Now let's create an invoice for your train tickets",
"agent: Train tickets booked! Please confirm the following invoice for the journey. <agent infers total amount for the invoice and details from the conversation history>",
"user_confirmed_tool_run: <user clicks confirm on CreateInvoice tool which includes details of the train journey, the match, and the total cost>",
"tool_result: contains an invoiceURL",
"agent: Great! I've generated your invoice for your trains to the <match>. You can view and pay your invoice at this link: https://invoice.stripe.com/i/acct_1NBOLuKVZbzw7QA5/test_YWNjdF8xTkJPTHVLVlpienc3UUE1LF9SaHlBTU9GYnFibEJ4VlpNaThkWkhrcUR6a1dwTmNULDEyOTE2MjkwNA0200CCUNvTox?s=ap",

View File

@@ -29,17 +29,17 @@ search_flights_tool = ToolDefinition(
search_trains_tool = ToolDefinition(
name="SearchTrains",
description="Search for trains between two stations. Returns a list of trains.",
description="Search for trains between two English cities. Returns a list of train information for the user to choose from.",
arguments=[
ToolArgument(
name="origin",
type="string",
description="The station to depart from",
description="The city or place to depart from",
),
ToolArgument(
name="destination",
type="string",
description="The station to arrive at",
description="The city or place to arrive at",
),
ToolArgument(
name="outbound_time",