From 59d3487a0a1dc166bb39b3b37c6c4a333bb187b9 Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Fri, 27 Dec 2024 00:33:24 +0300 Subject: [PATCH 1/5] chore(agents-api): Remove tools from default system message --- agents-api/agents_api/autogen/Sessions.py | 10 ++--- typespec/common/constants.tsp | 9 ---- .../@typespec/openapi3/openapi-1.0.0.yaml | 45 ------------------- 3 files changed, 5 insertions(+), 59 deletions(-) diff --git a/agents-api/agents_api/autogen/Sessions.py b/agents-api/agents_api/autogen/Sessions.py index e2a9ce164..6cd0ce10a 100644 --- a/agents-api/agents_api/autogen/Sessions.py +++ b/agents-api/agents_api/autogen/Sessions.py @@ -27,7 +27,7 @@ class CreateSessionRequest(BaseModel): Agent ID of agent associated with this session """ agents: list[UUID] | None = None - situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if tools -%}\nTools:{{NEWLINE}}\n {%- for tool in tools -%}\n - {{tool.name + NEWLINE}}\n {%- if tool.description -%}: {{tool.description + NEWLINE}}{%- endif -%}\n {%- endfor -%}\n{{NEWLINE+NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' + situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' """ A specific situation that sets the background for this session """ @@ -71,7 +71,7 @@ class PatchSessionRequest(BaseModel): model_config = ConfigDict( populate_by_name=True, ) - situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if tools -%}\nTools:{{NEWLINE}}\n {%- for tool in tools -%}\n - {{tool.name + NEWLINE}}\n {%- if tool.description -%}: {{tool.description + NEWLINE}}{%- endif -%}\n {%- endfor -%}\n{{NEWLINE+NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' + situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' """ A specific situation that sets the background for this session """ @@ -133,7 +133,7 @@ class Session(BaseModel): model_config = ConfigDict( populate_by_name=True, ) - situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if tools -%}\nTools:{{NEWLINE}}\n {%- for tool in tools -%}\n - {{tool.name + NEWLINE}}\n {%- if tool.description -%}: {{tool.description + NEWLINE}}{%- endif -%}\n {%- endfor -%}\n{{NEWLINE+NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' + situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' """ A specific situation that sets the background for this session """ @@ -217,7 +217,7 @@ class UpdateSessionRequest(BaseModel): model_config = ConfigDict( populate_by_name=True, ) - situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if tools -%}\nTools:{{NEWLINE}}\n {%- for tool in tools -%}\n - {{tool.name + NEWLINE}}\n {%- if tool.description -%}: {{tool.description + NEWLINE}}{%- endif -%}\n {%- endfor -%}\n{{NEWLINE+NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' + situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' """ A specific situation that sets the background for this session """ @@ -268,7 +268,7 @@ class CreateOrUpdateSessionRequest(CreateSessionRequest): Agent ID of agent associated with this session """ agents: list[UUID] | None = None - situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if tools -%}\nTools:{{NEWLINE}}\n {%- for tool in tools -%}\n - {{tool.name + NEWLINE}}\n {%- if tool.description -%}: {{tool.description + NEWLINE}}{%- endif -%}\n {%- endfor -%}\n{{NEWLINE+NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' + situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' """ A specific situation that sets the background for this session """ diff --git a/typespec/common/constants.tsp b/typespec/common/constants.tsp index bcd9e8bc1..b81570b3d 100644 --- a/typespec/common/constants.tsp +++ b/typespec/common/constants.tsp @@ -34,15 +34,6 @@ Instructions:{{NEWLINE}} {{NEWLINE}} {%- endif -%} -{%- if tools -%} -Tools:{{NEWLINE}} - {%- for tool in tools -%} - - {{tool.name + NEWLINE}} - {%- if tool.description -%}: {{tool.description + NEWLINE}}{%- endif -%} - {%- endfor -%} -{{NEWLINE+NEWLINE}} -{%- endif -%} - {%- if docs -%} Relevant documents:{{NEWLINE}} {%- for doc in docs -%} diff --git a/typespec/tsp-output/@typespec/openapi3/openapi-1.0.0.yaml b/typespec/tsp-output/@typespec/openapi3/openapi-1.0.0.yaml index bfb9e48fc..22c54f3ae 100644 --- a/typespec/tsp-output/@typespec/openapi3/openapi-1.0.0.yaml +++ b/typespec/tsp-output/@typespec/openapi3/openapi-1.0.0.yaml @@ -3839,15 +3839,6 @@ components: {{NEWLINE}} {%- endif -%} - {%- if tools -%} - Tools:{{NEWLINE}} - {%- for tool in tools -%} - - {{tool.name + NEWLINE}} - {%- if tool.description -%}: {{tool.description + NEWLINE}}{%- endif -%} - {%- endfor -%} - {{NEWLINE+NEWLINE}} - {%- endif -%} - {%- if docs -%} Relevant documents:{{NEWLINE}} {%- for doc in docs -%} @@ -3967,15 +3958,6 @@ components: {{NEWLINE}} {%- endif -%} - {%- if tools -%} - Tools:{{NEWLINE}} - {%- for tool in tools -%} - - {{tool.name + NEWLINE}} - {%- if tool.description -%}: {{tool.description + NEWLINE}}{%- endif -%} - {%- endfor -%} - {{NEWLINE+NEWLINE}} - {%- endif -%} - {%- if docs -%} Relevant documents:{{NEWLINE}} {%- for doc in docs -%} @@ -4115,15 +4097,6 @@ components: {{NEWLINE}} {%- endif -%} - {%- if tools -%} - Tools:{{NEWLINE}} - {%- for tool in tools -%} - - {{tool.name + NEWLINE}} - {%- if tool.description -%}: {{tool.description + NEWLINE}}{%- endif -%} - {%- endfor -%} - {{NEWLINE+NEWLINE}} - {%- endif -%} - {%- if docs -%} Relevant documents:{{NEWLINE}} {%- for doc in docs -%} @@ -4284,15 +4257,6 @@ components: {{NEWLINE}} {%- endif -%} - {%- if tools -%} - Tools:{{NEWLINE}} - {%- for tool in tools -%} - - {{tool.name + NEWLINE}} - {%- if tool.description -%}: {{tool.description + NEWLINE}}{%- endif -%} - {%- endfor -%} - {{NEWLINE+NEWLINE}} - {%- endif -%} - {%- if docs -%} Relevant documents:{{NEWLINE}} {%- for doc in docs -%} @@ -4462,15 +4426,6 @@ components: {{NEWLINE}} {%- endif -%} - {%- if tools -%} - Tools:{{NEWLINE}} - {%- for tool in tools -%} - - {{tool.name + NEWLINE}} - {%- if tool.description -%}: {{tool.description + NEWLINE}}{%- endif -%} - {%- endfor -%} - {{NEWLINE+NEWLINE}} - {%- endif -%} - {%- if docs -%} Relevant documents:{{NEWLINE}} {%- for doc in docs -%} From 718d612892ca1e7c1f0605c457ec2026e089d717 Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Fri, 27 Dec 2024 02:57:12 +0300 Subject: [PATCH 2/5] chore(agents-api): Swap `situation` and `system_template` values in `sessions` --- agents-api/agents_api/autogen/Sessions.py | 40 ++++----- .../agents_api/routers/sessions/chat.py | 4 +- typespec/common/constants.tsp | 6 ++ typespec/sessions/models.tsp | 8 +- .../@typespec/openapi3/openapi-1.0.0.yaml | 90 ++++++++++++------- 5 files changed, 92 insertions(+), 56 deletions(-) diff --git a/agents-api/agents_api/autogen/Sessions.py b/agents-api/agents_api/autogen/Sessions.py index 6cd0ce10a..20c9885b1 100644 --- a/agents-api/agents_api/autogen/Sessions.py +++ b/agents-api/agents_api/autogen/Sessions.py @@ -27,13 +27,13 @@ class CreateSessionRequest(BaseModel): Agent ID of agent associated with this session """ agents: list[UUID] | None = None - situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' + situation: str | None = None """ - A specific situation that sets the background for this session + Session situation """ - system_template: str | None = None + system_template: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE}}\n\n{%- if session.situation -%}\nSituation: {{session.situation}}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' """ - System prompt for this session + A specific system prompt template that sets the background for this session """ render_templates: StrictBool = True """ @@ -71,13 +71,13 @@ class PatchSessionRequest(BaseModel): model_config = ConfigDict( populate_by_name=True, ) - situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' + situation: str | None = None """ - A specific situation that sets the background for this session + Session situation """ - system_template: str | None = None + system_template: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE}}\n\n{%- if session.situation -%}\nSituation: {{session.situation}}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' """ - System prompt for this session + A specific system prompt template that sets the background for this session """ render_templates: StrictBool = True """ @@ -133,13 +133,13 @@ class Session(BaseModel): model_config = ConfigDict( populate_by_name=True, ) - situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' + situation: str | None = None """ - A specific situation that sets the background for this session + Session situation """ - system_template: str | None = None + system_template: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE}}\n\n{%- if session.situation -%}\nSituation: {{session.situation}}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' """ - System prompt for this session + A specific system prompt template that sets the background for this session """ summary: Annotated[str | None, Field(json_schema_extra={"readOnly": True})] = None """ @@ -217,13 +217,13 @@ class UpdateSessionRequest(BaseModel): model_config = ConfigDict( populate_by_name=True, ) - situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' + situation: str | None = None """ - A specific situation that sets the background for this session + Session situation """ - system_template: str | None = None + system_template: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE}}\n\n{%- if session.situation -%}\nSituation: {{session.situation}}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' """ - System prompt for this session + A specific system prompt template that sets the background for this session """ render_templates: StrictBool = True """ @@ -268,13 +268,13 @@ class CreateOrUpdateSessionRequest(CreateSessionRequest): Agent ID of agent associated with this session """ agents: list[UUID] | None = None - situation: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' + situation: str | None = None """ - A specific situation that sets the background for this session + Session situation """ - system_template: str | None = None + system_template: str = '{%- if agent.name -%}\nYou are {{agent.name}}.{{" "}}\n{%- endif -%}\n\n{%- if agent.about -%}\nAbout you: {{agent.about}}.{{" "}}\n{%- endif -%}\n\n{%- if user -%}\nYou are talking to a user\n {%- if user.name -%}{{" "}} and their name is {{user.name}}\n {%- if user.about -%}. About the user: {{user.about}}.{%- else -%}.{%- endif -%}\n {%- endif -%}\n{%- endif -%}\n\n{{NEWLINE}}\n\n{%- if session.situation -%}\nSituation: {{session.situation}}\n{%- endif -%}\n\n{{NEWLINE+NEWLINE}}\n\n{%- if agent.instructions -%}\nInstructions:{{NEWLINE}}\n {%- if agent.instructions is string -%}\n {{agent.instructions}}{{NEWLINE}}\n {%- else -%}\n {%- for instruction in agent.instructions -%}\n - {{instruction}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{NEWLINE}}\n{%- endif -%}\n\n{%- if docs -%}\nRelevant documents:{{NEWLINE}}\n {%- for doc in docs -%}\n {{doc.title}}{{NEWLINE}}\n {%- if doc.content is string -%}\n {{doc.content}}{{NEWLINE}}\n {%- else -%}\n {%- for snippet in doc.content -%}\n {{snippet}}{{NEWLINE}}\n {%- endfor -%}\n {%- endif -%}\n {{"---"}}\n {%- endfor -%}\n{%- endif -%}' """ - System prompt for this session + A specific system prompt template that sets the background for this session """ render_templates: StrictBool = True """ diff --git a/agents-api/agents_api/routers/sessions/chat.py b/agents-api/agents_api/routers/sessions/chat.py index a5716fcdb..2fc5a859e 100644 --- a/agents-api/agents_api/routers/sessions/chat.py +++ b/agents-api/agents_api/routers/sessions/chat.py @@ -96,10 +96,10 @@ async def chat( for ref in doc_references ] # Render the system message - if situation := chat_context.session.situation: + if system_template := chat_context.session.system_template: system_message = dict( role="system", - content=situation, + content=system_template, ) system_messages: list[dict] = await render_template( diff --git a/typespec/common/constants.tsp b/typespec/common/constants.tsp index b81570b3d..da9ed226b 100644 --- a/typespec/common/constants.tsp +++ b/typespec/common/constants.tsp @@ -20,6 +20,12 @@ You are talking to a user {%- endif -%} {%- endif -%} +{{NEWLINE}} + +{%- if session.situation -%} +Situation: {{session.situation}} +{%- endif -%} + {{NEWLINE+NEWLINE}} {%- if agent.instructions -%} diff --git a/typespec/sessions/models.tsp b/typespec/sessions/models.tsp index 720625f3b..68b328af0 100644 --- a/typespec/sessions/models.tsp +++ b/typespec/sessions/models.tsp @@ -60,11 +60,11 @@ model Session { @visibility("create") agents?: uuid[]; - /** A specific situation that sets the background for this session */ - situation: string = defaultSessionSystemMessage; + /** Session situation */ + situation: string | null = null; - /** System prompt for this session */ - system_template: string | null = null; + /** A specific system prompt template that sets the background for this session */ + system_template: string = defaultSessionSystemMessage; /** Summary (null at the beginning) - generated automatically after every interaction */ @visibility("read") diff --git a/typespec/tsp-output/@typespec/openapi3/openapi-1.0.0.yaml b/typespec/tsp-output/@typespec/openapi3/openapi-1.0.0.yaml index 22c54f3ae..d9aab47ee 100644 --- a/typespec/tsp-output/@typespec/openapi3/openapi-1.0.0.yaml +++ b/typespec/tsp-output/@typespec/openapi3/openapi-1.0.0.yaml @@ -3808,7 +3808,12 @@ components: $ref: '#/components/schemas/Common.uuid' situation: type: string - description: A specific situation that sets the background for this session + nullable: true + description: Session situation + default: null + system_template: + type: string + description: A specific system prompt template that sets the background for this session default: |- {%- if agent.name -%} You are {{agent.name}}.{{" "}} @@ -3825,6 +3830,12 @@ components: {%- endif -%} {%- endif -%} + {{NEWLINE}} + + {%- if session.situation -%} + Situation: {{session.situation}} + {%- endif -%} + {{NEWLINE+NEWLINE}} {%- if agent.instructions -%} @@ -3853,11 +3864,6 @@ components: {{"---"}} {%- endfor -%} {%- endif -%} - system_template: - type: string - nullable: true - description: System prompt for this session - default: null render_templates: type: boolean description: Render system and assistant message content as jinja templates @@ -3927,7 +3933,12 @@ components: $ref: '#/components/schemas/Common.uuid' situation: type: string - description: A specific situation that sets the background for this session + nullable: true + description: Session situation + default: null + system_template: + type: string + description: A specific system prompt template that sets the background for this session default: |- {%- if agent.name -%} You are {{agent.name}}.{{" "}} @@ -3944,6 +3955,12 @@ components: {%- endif -%} {%- endif -%} + {{NEWLINE}} + + {%- if session.situation -%} + Situation: {{session.situation}} + {%- endif -%} + {{NEWLINE+NEWLINE}} {%- if agent.instructions -%} @@ -3972,11 +3989,6 @@ components: {{"---"}} {%- endfor -%} {%- endif -%} - system_template: - type: string - nullable: true - description: System prompt for this session - default: null render_templates: type: boolean description: Render system and assistant message content as jinja templates @@ -4066,7 +4078,12 @@ components: properties: situation: type: string - description: A specific situation that sets the background for this session + nullable: true + description: Session situation + default: null + system_template: + type: string + description: A specific system prompt template that sets the background for this session default: |- {%- if agent.name -%} You are {{agent.name}}.{{" "}} @@ -4083,6 +4100,12 @@ components: {%- endif -%} {%- endif -%} + {{NEWLINE}} + + {%- if session.situation -%} + Situation: {{session.situation}} + {%- endif -%} + {{NEWLINE+NEWLINE}} {%- if agent.instructions -%} @@ -4111,11 +4134,6 @@ components: {{"---"}} {%- endfor -%} {%- endif -%} - system_template: - type: string - nullable: true - description: System prompt for this session - default: null render_templates: type: boolean description: Render system and assistant message content as jinja templates @@ -4226,7 +4244,12 @@ components: properties: situation: type: string - description: A specific situation that sets the background for this session + nullable: true + description: Session situation + default: null + system_template: + type: string + description: A specific system prompt template that sets the background for this session default: |- {%- if agent.name -%} You are {{agent.name}}.{{" "}} @@ -4243,6 +4266,12 @@ components: {%- endif -%} {%- endif -%} + {{NEWLINE}} + + {%- if session.situation -%} + Situation: {{session.situation}} + {%- endif -%} + {{NEWLINE+NEWLINE}} {%- if agent.instructions -%} @@ -4271,11 +4300,6 @@ components: {{"---"}} {%- endfor -%} {%- endif -%} - system_template: - type: string - nullable: true - description: System prompt for this session - default: null summary: type: string nullable: true @@ -4395,7 +4419,12 @@ components: properties: situation: type: string - description: A specific situation that sets the background for this session + nullable: true + description: Session situation + default: null + system_template: + type: string + description: A specific system prompt template that sets the background for this session default: |- {%- if agent.name -%} You are {{agent.name}}.{{" "}} @@ -4412,6 +4441,12 @@ components: {%- endif -%} {%- endif -%} + {{NEWLINE}} + + {%- if session.situation -%} + Situation: {{session.situation}} + {%- endif -%} + {{NEWLINE+NEWLINE}} {%- if agent.instructions -%} @@ -4440,11 +4475,6 @@ components: {{"---"}} {%- endfor -%} {%- endif -%} - system_template: - type: string - nullable: true - description: System prompt for this session - default: null render_templates: type: boolean description: Render system and assistant message content as jinja templates From 23de839a1c57e024a776f527ee694e78ecb26389 Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Fri, 27 Dec 2024 03:01:16 +0300 Subject: [PATCH 3/5] fix(agents-api): Misc fixes for chat endpoint --- .../agents_api/common/protocol/sessions.py | 1 + .../queries/chat/gather_messages.py | 12 ++++----- .../queries/chat/prepare_chat_context.py | 27 ++++++++++++------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/agents-api/agents_api/common/protocol/sessions.py b/agents-api/agents_api/common/protocol/sessions.py index 3b04178e1..0960e7336 100644 --- a/agents-api/agents_api/common/protocol/sessions.py +++ b/agents-api/agents_api/common/protocol/sessions.py @@ -117,6 +117,7 @@ def get_chat_environment(self) -> dict[str, dict | list[dict] | None]: "agents": [agent.model_dump() for agent in self.agents], "current_agent": current_agent.model_dump(), "agent": current_agent.model_dump(), + "user": self.users[0].model_dump() if len(self.users) > 0 else None, "users": [user.model_dump() for user in self.users], "settings": settings, "tools": [tool.model_dump() for tool in tools], diff --git a/agents-api/agents_api/queries/chat/gather_messages.py b/agents-api/agents_api/queries/chat/gather_messages.py index cbf3bf209..fb3205acf 100644 --- a/agents-api/agents_api/queries/chat/gather_messages.py +++ b/agents-api/agents_api/queries/chat/gather_messages.py @@ -5,7 +5,7 @@ from fastapi import HTTPException from pydantic import ValidationError -from ...autogen.openapi_model import ChatInput, DocReference, History +from ...autogen.openapi_model import ChatInput, DocReference, History, Session from ...clients import litellm from ...common.protocol.developers import Developer from ...common.protocol.sessions import ChatContext @@ -42,7 +42,7 @@ async def gather_messages( assert len(new_raw_messages) > 0 # Get the session history - history: History = get_history( + history: History = await get_history( developer_id=developer.id, session_id=session_id, allowed_sources=["api_request", "api_response", "tool_response", "summarizer"], @@ -69,7 +69,7 @@ async def gather_messages( return past_messages, [] # Get recall options - session = get_session( + session: Session = await get_session( developer_id=developer.id, session_id=session_id, ) @@ -117,20 +117,20 @@ async def gather_messages( doc_references: list[DocReference] = [] match recall_options.mode: case "vector": - doc_references: list[DocReference] = search_docs_by_embedding( + doc_references: list[DocReference] = await search_docs_by_embedding( developer_id=developer.id, owners=owners, query_embedding=query_embedding, ) case "hybrid": - doc_references: list[DocReference] = search_docs_hybrid( + doc_references: list[DocReference] = await search_docs_hybrid( developer_id=developer.id, owners=owners, query=query_text, query_embedding=query_embedding, ) case "text": - doc_references: list[DocReference] = search_docs_by_text( + doc_references: list[DocReference] = await search_docs_by_text( developer_id=developer.id, owners=owners, query=query_text, diff --git a/agents-api/agents_api/queries/chat/prepare_chat_context.py b/agents-api/agents_api/queries/chat/prepare_chat_context.py index c3a8b8ba3..9df7e3273 100644 --- a/agents-api/agents_api/queries/chat/prepare_chat_context.py +++ b/agents-api/agents_api/queries/chat/prepare_chat_context.py @@ -2,7 +2,7 @@ from uuid import UUID from beartype import beartype - +from sqlglot import parse_one from ...common.protocol.sessions import ChatContext, make_session from ..utils import ( pg_query, @@ -13,7 +13,7 @@ T = TypeVar("T") -sql_query = """ +sql_query = parse_one(""" SELECT * FROM ( SELECT jsonb_agg(u) AS users FROM ( @@ -65,6 +65,7 @@ sessions.situation, sessions.system_template, sessions.created_at, + sessions.updated_at, sessions.metadata, sessions.render_templates, sessions.token_budget, @@ -86,7 +87,6 @@ tools.developer_id, tools.agent_id, tools.task_id, - tools.task_version, tools.type, tools.name, tools.description, @@ -100,23 +100,28 @@ session_id = $2 AND session_lookup.participant_type = 'agent' ) r -) AS toolsets""" +) AS toolsets""").sql(pretty=True) def _transform(d): toolsets = {} - for tool in d["toolsets"]: + + # Default to empty lists when users/agents are not present + d["users"] = d.get("users") or [] + d["agents"] = d.get("agents") or [] + + for tool in d.get("toolsets") or []: agent_id = tool["agent_id"] if agent_id in toolsets: toolsets[agent_id].append(tool) else: toolsets[agent_id] = [tool] - return { + transformed_data = { **d, "session": make_session( - agents=[a["id"] for a in d["agents"]], - users=[u["id"] for u in d["users"]], + agents=[a["id"] for a in d.get("agents") or []], + users=[u["id"] for u in d.get("users") or []], **d["session"], ), "toolsets": [ @@ -134,6 +139,8 @@ def _transform(d): ], } + return transformed_data + # TODO: implement this part # @rewrap_exceptions( @@ -153,12 +160,12 @@ async def prepare_chat_context( *, developer_id: UUID, session_id: UUID, -) -> tuple[list[str], list]: +) -> tuple[str, list]: """ Executes a complex query to retrieve memory context based on session ID. """ return ( - [sql_query.format()], + sql_query, [developer_id, session_id], ) From 40c1a82516df21a9a45b5f83591846dd560be9ab Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Fri, 27 Dec 2024 00:02:28 +0000 Subject: [PATCH 4/5] refactor: Lint agents-api (CI) --- agents-api/agents_api/queries/chat/prepare_chat_context.py | 1 + 1 file changed, 1 insertion(+) diff --git a/agents-api/agents_api/queries/chat/prepare_chat_context.py b/agents-api/agents_api/queries/chat/prepare_chat_context.py index 9df7e3273..241f7ad25 100644 --- a/agents-api/agents_api/queries/chat/prepare_chat_context.py +++ b/agents-api/agents_api/queries/chat/prepare_chat_context.py @@ -3,6 +3,7 @@ from beartype import beartype from sqlglot import parse_one + from ...common.protocol.sessions import ChatContext, make_session from ..utils import ( pg_query, From 9e82981967041e479f1b9b606161793dc2c7aabf Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Fri, 27 Dec 2024 08:38:54 +0300 Subject: [PATCH 5/5] Remove `sqlglot` --- agents-api/agents_api/queries/chat/prepare_chat_context.py | 5 ++--- drafts/cozo | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) create mode 160000 drafts/cozo diff --git a/agents-api/agents_api/queries/chat/prepare_chat_context.py b/agents-api/agents_api/queries/chat/prepare_chat_context.py index 241f7ad25..e56e66abe 100644 --- a/agents-api/agents_api/queries/chat/prepare_chat_context.py +++ b/agents-api/agents_api/queries/chat/prepare_chat_context.py @@ -2,7 +2,6 @@ from uuid import UUID from beartype import beartype -from sqlglot import parse_one from ...common.protocol.sessions import ChatContext, make_session from ..utils import ( @@ -14,7 +13,7 @@ T = TypeVar("T") -sql_query = parse_one(""" +sql_query = """ SELECT * FROM ( SELECT jsonb_agg(u) AS users FROM ( @@ -101,7 +100,7 @@ session_id = $2 AND session_lookup.participant_type = 'agent' ) r -) AS toolsets""").sql(pretty=True) +) AS toolsets""" def _transform(d): diff --git a/drafts/cozo b/drafts/cozo new file mode 160000 index 000000000..faf89ef77 --- /dev/null +++ b/drafts/cozo @@ -0,0 +1 @@ +Subproject commit faf89ef77e6462460f873e9de618001d968a1a40