Skip to content

Commit beb433b

Browse files
committed
Move to Uvicorn, use few shots
1 parent ded7bf4 commit beb433b

File tree

10 files changed

+61
-27
lines changed

10 files changed

+61
-27
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
"name": "Backend",
1818
"type": "debugpy",
1919
"request": "launch",
20+
"cwd": "${workspaceFolder}",
2021
"module": "uvicorn",
2122
"args": ["fastapi_app:create_app", "--factory", "--reload"],
22-
"justMyCode": true
23+
"justMyCode": false
2324
}
2425
],
2526
"compounds": [

src/backend/Dockerfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM mcr.microsoft.com/devcontainers/python:3.12-bullseye
1+
FROM python:3.12-bullseye
22

33
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
44
&& apt-get -y install --no-install-recommends postgresql-client \
@@ -12,8 +12,9 @@ WORKDIR /demo-code
1212
COPY requirements.txt .
1313
RUN python -m pip install -r requirements.txt
1414

15-
COPY entrypoint.sh .
16-
RUN chmod +x entrypoint.sh
17-
1815
COPY . .
19-
CMD bash -c ". entrypoint.sh"
16+
RUN python -m pip install .
17+
18+
RUN chmod +x entrypoint.sh
19+
EXPOSE 8000
20+
CMD ["bash", "-c", ". entrypoint.sh"]

src/backend/entrypoint.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
#!/bin/bash
22
set -e
3-
python3 -m pip install .
4-
python3 -m gunicorn "fastapi_app:create_app()"
3+
python3 -m uvicorn "fastapi_app:create_app" --factory --port 8000

src/backend/fastapi_app/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ async def lifespan(app: fastapi.FastAPI) -> AsyncIterator[State]:
4747

4848
def create_app(testing: bool = False):
4949
if os.getenv("RUNNING_IN_PRODUCTION"):
50-
logging.basicConfig(level=logging.WARNING)
50+
# You may choose to reduce this to logging.WARNING for production
51+
logging.basicConfig(level=logging.INFO)
5152
else:
5253
if not testing:
5354
load_dotenv(override=True)
5455
logging.basicConfig(level=logging.INFO)
5556
# Turn off particularly noisy INFO level logs from Azure Core SDK:
5657
logging.getLogger("azure.core.pipeline.policies.http_logging_policy").setLevel(logging.WARNING)
58+
logging.getLogger("azure.identity").setLevel(logging.WARNING)
5759

5860
if os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"):
5961
logger.info("Configuring Azure Monitor")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[
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+
}
34+
]

src/backend/fastapi_app/rag_advanced.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,19 @@ async def generate_search_query(
3838
self, original_user_query: str, past_messages: list[ChatCompletionMessageParam], query_response_token_limit: int
3939
) -> tuple[list[ChatCompletionMessageParam], Any | str | None, list]:
4040
"""Generate an optimized keyword search query based on the chat history and the last question"""
41+
42+
tools = build_search_function()
43+
tool_choice = "auto"
44+
4145
query_messages: list[ChatCompletionMessageParam] = build_messages(
4246
model=self.chat_model,
4347
system_prompt=self.query_prompt_template,
48+
few_shots=self.query_fewshots,
4449
new_user_content=original_user_query,
4550
past_messages=past_messages,
46-
max_tokens=self.chat_token_limit - query_response_token_limit, # TODO: count functions
51+
max_tokens=self.chat_token_limit - query_response_token_limit,
52+
tools=tools,
53+
tool_choice=tool_choice,
4754
fallback_to_default=True,
4855
)
4956

@@ -54,8 +61,8 @@ async def generate_search_query(
5461
temperature=0.0, # Minimize creativity for search query generation
5562
max_tokens=query_response_token_limit, # Setting too low risks malformed JSON, too high risks performance
5663
n=1,
57-
tools=build_search_function(),
58-
tool_choice="auto",
64+
tools=tools,
65+
tool_choice=tool_choice,
5966
)
6067

6168
query_text, filters = extract_search_arguments(original_user_query, chat_completion)

src/backend/fastapi_app/rag_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import pathlib
23
from abc import ABC, abstractmethod
34
from collections.abc import AsyncGenerator
@@ -17,6 +18,7 @@
1718
class RAGChatBase(ABC):
1819
current_dir = pathlib.Path(__file__).parent
1920
query_prompt_template = open(current_dir / "prompts/query.txt").read()
21+
query_fewshots = json.loads(open(current_dir / "prompts/query_fewshots.json").read())
2022
answer_prompt_template = open(current_dir / "prompts/answer.txt").read()
2123

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

src/backend/gunicorn.conf.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/backend/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
22
name = "fastapi_app"
33
version = "1.0.0"
4-
description = "Create a application with fastapi and postgres-flexible"
4+
description = "Create a RAG application with FastAPI and PostgreSQL"
55
dependencies = [
66
"fastapi>=0.111.0,<1.0.0",
77
"python-dotenv>=1.0.1,<2.0.0",
@@ -13,7 +13,7 @@ dependencies = [
1313
"pgvector>=0.2.5,<0.3.0",
1414
"openai>=1.34.0,<2.0.0",
1515
"tiktoken>=0.7.0,<0.8.0",
16-
"openai-messages-token-helper>=0.1.5,<0.2.0",
16+
"openai-messages-token-helper>=0.1.7,<0.2.0",
1717
"azure-monitor-opentelemetry>=1.6.0,<2.0.0",
1818
"opentelemetry-instrumentation-sqlalchemy>=0.46b0,<1.0.0",
1919
"opentelemetry-instrumentation-aiohttp-client>=0.46b0,<1.0.0",

src/backend/requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
gunicorn>=22.0.0,<23.0.0
2-
uvicorn>=0.30.1,<1.0.0
1+
uvicorn>=0.30.1,<1.0.0

0 commit comments

Comments
 (0)