Skip to content

Commit

Permalink
feat(cookbooks): cookbook 10,11 bugs fixed but output is incorrect. 1…
Browse files Browse the repository at this point in the history
…2 & 13 needs fixing
  • Loading branch information
Vedantsahai18 committed Oct 9, 2024
1 parent 22678a7 commit 9894aa6
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 94 deletions.
2 changes: 2 additions & 0 deletions cookbooks/08-Managing_Persistent_Sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def user_interaction(prompt):
# Get the execution result
result = client.executions.get(execution.id)

# Wait for the execution to complete
time.sleep(2)

# Update the context and print the response
Expand Down Expand Up @@ -133,6 +134,7 @@ def user_interaction(prompt):
)

overflow_result = client.executions.get(overflow_execution.id)
# Wait for the execution to complete
time.sleep(2)
overflow_response = client.executions.transitions.list(execution_id=overflow_result.id).items[0].output
print(f"Agent response to large input: {overflow_response['session_context']['agent_response']}")
Expand Down
2 changes: 2 additions & 0 deletions cookbooks/09-User_Management_and_Personalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def register_user(username, interests):
"interests": interests
}
)
# Wait for the execution to complete
time.sleep(2)
result = client.executions.get(execution.id)
user_result = client.executions.transitions.list(execution_id=result.id).items[0].output
Expand All @@ -155,6 +156,7 @@ def get_recommendations(user_profile):
"content_list": content_list
}
)
# Wait for the execution to complete
time.sleep(2)
result = client.executions.get(execution.id)
recommendation_respose = client.executions.transitions.list(execution_id=result.id).items[0].output
Expand Down
26 changes: 16 additions & 10 deletions cookbooks/10-Document_Management_and_Search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# 7. Execute the document search task
# 8. Display the search results

# UNDER CONSTRUCTION - NOT WORKING YET
# UNDER CONSTRUCTION - YAML is working but the flow is not correct yet

import uuid
import yaml,time
Expand Down Expand Up @@ -45,17 +45,17 @@
items:
type: object
properties:
tile:
type: string
content:
type: string
metadata:
type: object
tools:
- name: document_create
type: system
system:
resource: agent
resource_id: "{{agent_id}}"
subresource: doc
operation: create
Expand All @@ -64,6 +64,7 @@
map:
tool: document_upload
arguments:
agent_id: "'{agent.id}'"
content: _.content
metadata: _.metadata
Expand Down Expand Up @@ -95,28 +96,28 @@
tools:
- name: document_search
type: system
system:
resource: agent
resource_id: "{{agent_id}}"
subresource: doc
operation: search
main:
- tool: document_search
arguments:
agent_id: "'{agent.id}'"
query: inputs[0].query
filters: inputs[0].filters
- prompt:
role: system
- role: system
content: >-
Based on the search results, provide a summary of the most relevant documents found.
Search query: {{inputs[0].query}}
Number of results: {{len(outputs[0])}}
Results:
{{outputs[0]}}
unwrap: true
""")

# Creating the search task
Expand Down Expand Up @@ -152,8 +153,12 @@
)

print("Uploading and indexing documents...")
# Wait for the execution to complete
time.sleep(5)
upload_result = client.executions.get(upload_execution.id)
print(upload_result.output)
upload_response = client.executions.transitions.list(upload_execution.id).items[0].output
print("Upload Result:")
print(upload_response)

# Execute the document search task
search_execution = client.executions.create(
Expand All @@ -165,9 +170,9 @@
)

print("\nSearching documents...")
# Wait for the execution to complete
time.sleep(5)
search_result = client.executions.get(search_execution.id)
print(search_result.output)

# Display the search results
print("\nSearch Results:")
for transition in client.executions.transitions.list(execution_id=search_execution.id).items:
Expand All @@ -176,4 +181,5 @@
print(f"- {doc['content']} (Score: {doc['score']})")

print("\nSearch Summary:")
print(search_result.output)
search_response = client.executions.transitions.list(search_result.id).items[0].output
print(search_response)
96 changes: 44 additions & 52 deletions cookbooks/11-Advanced_Chat_Interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# d. Integrating external information during the conversation
# 6. Display the chat history and any relevant metrics

# UNDER CONSTRUCTION - NOT WORKING YET
# UNDER CONSTRUCTION - YAML is working but the flow is not correct yet

import uuid
import yaml
Expand All @@ -39,18 +39,6 @@
model="gpt-4-turbo",
)

# Add a web search tool to the agent
client.agents.tools.create(
agent_id=AGENT_UUID,
name="web_search",
description="Search the web for information.",
integration={
"provider": "brave",
"method": "search",
"setup": {"api_key": "YOUR_BRAVE_API_KEY"},
},
)

# Defining a task for handling complex conversations
chat_task_def = yaml.safe_load("""
name: Advanced Chat Interaction
Expand All @@ -76,48 +64,49 @@
integration:
provider: weather
setup:
api_key: "YOUR_WEATHER_API_KEY"
api_key: "API_KEY"
main:
- evaluate:
context_length: len(inputs[0].chat_history)
- if:
condition: _.context_length > 10
then:
- evaluate:
summarized_history: "Summarize the following chat history: " + str(inputs[0].chat_history[-10:])
- prompt:
role: system
content: >-
You are an advanced chat assistant. Here's a summary of the recent conversation:
{{outputs[1].summarized_history}}
Now, respond to the user's latest input: {{inputs[0].user_input}}
else:
- prompt:
role: system
content: >-
You are an advanced chat assistant. Here's the conversation history:
{{inputs[0].chat_history}}
Now, respond to the user's latest input: {{inputs[0].user_input}}
- if:
condition: "weather" in inputs[0].user_input.lower()
then:
- tool: weather_api
arguments:
location: "New York"
- prompt:
role: system
content: >-
The user mentioned weather. Here's the current weather information for New York:
{{outputs[3]}}
Incorporate this information into your response.
- return: _
- if: "_.context_length > '10'"
then:
evaluate:
summarized_history: str(inputs[0].chat_history[-10:])
prompt:
- role: system
content: >-
You are an advanced chat assistant. Here's a summary of the recent conversation:
{{outputs[1].summarized_history}}
Now, respond to the user's latest input: {{inputs[0].user_input}}
unwrap: true
else:
prompt:
- role: system
content: >-
You are an advanced chat assistant. Here's the conversation history:
{{inputs[0].chat_history}}
Now, respond to the user's latest input: {{inputs[0].user_input}}
unwrap: true
- if: "'weather' in inputs[0].user_input.lower()"
then:
tool: weather_api
arguments:
location: inputs[0].user_input.lower.split('weather')[1].strip()
prompt:
- role: system
content: >-
The user mentioned weather. Here's the current weather information for {{inputs[0].user_input.lower.split('weather')[1].strip()}}
Incorporate this information into your response.
unwrap: true
- return:
summary: _
""")

# Creating the chat task
Expand All @@ -141,7 +130,7 @@ def run_chat_session():
chat_history = []
print("Starting advanced chat session. Type 'exit' to end the conversation.")

session = client.sessions.create(agent_id=AGENT_UUID)
session = client.sessions.create(agent=AGENT_UUID)

while True:
user_input = get_user_input()
Expand All @@ -157,8 +146,11 @@ def run_chat_session():
"chat_history": chat_history
}
)

# Wait for the execution to complete
time.sleep(3)
result = client.executions.get(execution.id)
print(client.executions.transitions.list(execution.id).items)
print(f"Execution result: {result.output}")
assistant_response = result.output

chat_history.append({"role": "assistant", "content": assistant_response})
Expand Down
66 changes: 34 additions & 32 deletions cookbooks/13-Error_Handling_and_Recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,45 +75,47 @@
type: string
main:
- switch:
- evaluate:
value: inputs[0].operation
cases:
divide:
- tool: divide
arguments:
divisor: inputs[0].value
on_error:
retry:
max_attempts: 3
delay: 2
fallback:
return: "Error: Division by zero or invalid input"
api_call:
- tool: api_call
arguments:
endpoint: "/status/{{inputs[0].value}}"
on_error:
retry:
max_attempts: 3
delay: 5
fallback:
return: "Error: API call failed after multiple attempts"
process_data:
- evaluate:
- switch:
case: "'divide'"
tool: divide
arguments:
divisor: inputs[0].value
on_error:
retry:
max_attempts: 3
delay: 2
fallback:
return: "Error: Division by zero or invalid input"
case: "'api_call'"
tool: api_call
arguments:
endpoint: "/status/{{inputs[0].value}}"
on_error:
retry:
max_attempts: 3
delay: 5
fallback:
return: "Error: API call failed after multiple attempts"
case: "'process_data'"
evaluate:
data: "'Sample data: ' + str(inputs[0].value)"
- tool: process_data
arguments:
data: _.data
on_error:
log: "Error occurred while processing data"
return: "Error: Data processing failed"
tool: process_data
arguments:
data: _.data
on_error:
log: "Error occurred while processing data"
return: "Error: Data processing failed"
- prompt:
role: system
- role: system
content: >-
Summarize the result of the operation:
Operation: {{inputs[0].operation}}
Result: {{_}}
Result: {{_}}]
unwrap: true
""")

# Creating the task
Expand Down

0 comments on commit 9894aa6

Please sign in to comment.