Skip to content

Commit 020434e

Browse files
committed
Port over fewshots to Pydantic format
1 parent 202fa4b commit 020434e

File tree

4 files changed

+81
-40
lines changed

4 files changed

+81
-40
lines changed
Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,76 @@
11
[
2-
{"role": "user", "content": "good options for climbing gear that can be used outside?"},
3-
{"role": "assistant", "tool_calls": [
4-
{
5-
"id": "call_abc123",
6-
"type": "function",
7-
"function": {
8-
"arguments": "{\"search_query\":\"climbing gear outside\"}",
9-
"name": "search_database"
10-
}
11-
}
12-
]},
13-
{
14-
"role": "tool",
15-
"tool_call_id": "call_abc123",
16-
"content": "Search results for climbing gear that can be used outside: ..."
17-
},
18-
{"role": "user", "content": "are there any shoes less than $50?"},
19-
{"role": "assistant", "tool_calls": [
20-
{
21-
"id": "call_abc456",
22-
"type": "function",
23-
"function": {
24-
"arguments": "{\"search_query\":\"shoes\",\"price_filter\":{\"comparison_operator\":\"<\",\"value\":50}}",
25-
"name": "search_database"
26-
}
27-
}
28-
]},
29-
{
30-
"role": "tool",
31-
"tool_call_id": "call_abc456",
32-
"content": "Search results for shoes cheaper than 50: ..."
33-
}
2+
{
3+
"parts": [
4+
{
5+
"content": "good options for climbing gear that can be used outside?",
6+
"timestamp": "2025-05-07T19:02:46.977501Z",
7+
"part_kind": "user-prompt"
8+
}
9+
],
10+
"instructions": null,
11+
"kind": "request"
12+
},
13+
{
14+
"parts": [
15+
{
16+
"tool_name": "search_database",
17+
"args": "{\"search_query\":\"climbing gear outside\"}",
18+
"tool_call_id": "call_4HeBCmo2uioV6CyoePEGyZPc",
19+
"part_kind": "tool-call"
20+
}
21+
],
22+
"model_name": "gpt-4o-mini-2024-07-18",
23+
"timestamp": "2025-05-07T19:02:47Z",
24+
"kind": "response"
25+
},
26+
{
27+
"parts": [
28+
{
29+
"tool_name": "search_database",
30+
"content": "Search results for climbing gear that can be used outside: ...",
31+
"tool_call_id": "call_4HeBCmo2uioV6CyoePEGyZPc",
32+
"timestamp": "2025-05-07T19:02:48.242408Z",
33+
"part_kind": "tool-return"
34+
}
35+
],
36+
"instructions": null,
37+
"kind": "request"
38+
},
39+
{
40+
"parts": [
41+
{
42+
"content": "are there any shoes less than $50?",
43+
"timestamp": "2025-05-07T19:02:46.977501Z",
44+
"part_kind": "user-prompt"
45+
}
46+
],
47+
"instructions": null,
48+
"kind": "request"
49+
},
50+
{
51+
"parts": [
52+
{
53+
"tool_name": "search_database",
54+
"args": "{\"search_query\":\"shoes\",\"price_filter\":{\"comparison_operator\":\"<\",\"value\":50}}",
55+
"tool_call_id": "call_4HeBCmo2uioV6CyoePEGyZPc",
56+
"part_kind": "tool-call"
57+
}
58+
],
59+
"model_name": "gpt-4o-mini-2024-07-18",
60+
"timestamp": "2025-05-07T19:02:47Z",
61+
"kind": "response"
62+
},
63+
{
64+
"parts": [
65+
{
66+
"tool_name": "search_database",
67+
"content": "Search results for shoes cheaper than 50: ...",
68+
"tool_call_id": "call_4HeBCmo2uioV6CyoePEGyZPc",
69+
"timestamp": "2025-05-07T19:02:48.242408Z",
70+
"part_kind": "tool-return"
71+
}
72+
],
73+
"instructions": null,
74+
"kind": "request"
75+
}
3476
]

src/backend/fastapi_app/rag_advanced.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from openai.types.chat import ChatCompletionChunk, ChatCompletionMessageParam
77
from openai_messages_token_helper import get_token_limit
88
from pydantic_ai import Agent, RunContext
9+
from pydantic_ai.messages import ModelMessagesTypeAdapter
910
from pydantic_ai.models.openai import OpenAIModel
1011
from pydantic_ai.providers.openai import OpenAIProvider
1112
from pydantic_ai.settings import ModelSettings
@@ -119,15 +120,15 @@ async def prepare_context(self, chat_params: ChatParams) -> tuple[list[ItemPubli
119120
agent = Agent(
120121
model,
121122
model_settings=ModelSettings(temperature=0.0, max_tokens=500, seed=chat_params.seed),
122-
system_prompt=self.query_prompt_template,
123+
instructions=self.query_prompt_template,
123124
tools=[self.search_database],
124125
output_type=SearchResults,
125126
)
126-
# TODO: Provide few-shot examples
127+
few_shots = ModelMessagesTypeAdapter.validate_json(self.query_fewshots)
127128
user_query = f"Find search results for user query: {chat_params.original_user_query}"
128129
results = await agent.run(
129130
user_query,
130-
message_history=chat_params.past_messages,
131+
message_history=few_shots + chat_params.past_messages,
131132
deps=chat_params,
132133
)
133134
items = results.output["items"]
@@ -175,9 +176,9 @@ async def answer(
175176
),
176177
)
177178

178-
item_references = [item.to_str_for_rag() for item in items]
179+
sources_content = [f"[{(item.id)}]:{item.to_str_for_rag()}\n\n" for item in items]
179180
response = await agent.run(
180-
user_prompt=chat_params.original_user_query + "Sources:\n" + "\n".join(item_references),
181+
user_prompt=chat_params.original_user_query + "Sources:\n" + "\n".join(sources_content),
181182
message_history=chat_params.past_messages,
182183
)
183184

src/backend/fastapi_app/rag_base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import pathlib
32
from abc import ABC, abstractmethod
43
from collections.abc import AsyncGenerator
@@ -18,7 +17,7 @@
1817
class RAGChatBase(ABC):
1918
current_dir = pathlib.Path(__file__).parent
2019
query_prompt_template = open(current_dir / "prompts/query.txt").read()
21-
query_fewshots = json.loads(open(current_dir / "prompts/query_fewshots.json").read())
20+
query_fewshots = open(current_dir / "prompts/query_fewshots.json").read()
2221
answer_prompt_template = open(current_dir / "prompts/answer.txt").read()
2322

2423
def get_params(self, messages: list[ChatCompletionMessageParam], overrides: ChatRequestOverrides) -> ChatParams:

src/frontend/src/components/Answer/Answer.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export const Answer = ({
3434
const parsedAnswer = useMemo(() => parseAnswerToHtml(messageContent, isStreaming, onCitationClicked), [answer]);
3535

3636
const sanitizedAnswerHtml = DOMPurify.sanitize(parsedAnswer.answerHtml);
37-
3837
return (
3938
<Stack className={`${styles.answerContainer} ${isSelected && styles.selected}`} verticalAlign="space-between">
4039
<Stack.Item>

0 commit comments

Comments
 (0)