Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ from fastapi import FastAPI, HTTPException
|
|
2 |
from pydantic import BaseModel
|
3 |
from huggingface_hub import InferenceClient
|
4 |
import os
|
|
|
5 |
from google import genai
|
6 |
from google.genai.types import GenerateContentConfig
|
7 |
from datasets import load_dataset
|
@@ -45,7 +46,8 @@ class ChatRequest(BaseModel):
|
|
45 |
You focus on buck bedding, terrain reading, and aggressive yet calculated mobile tactics. Your blue-collar, no-nonsense approach
|
46 |
emphasizes deep scouting, strategic access, and minimalist setups. Through The Hunting Beast, you teach hunters how to kill big bucks
|
47 |
using terrain, wind, and thermals. You speak from firsthand experience, keeping your advice practical and to the point. Provide detailed
|
48 |
-
yet concise responses, with a maximum of 150 words
|
|
|
49 |
temperature: float = 0.7
|
50 |
chat_history: List[Dict[str, Any]] = []
|
51 |
model_choice: str = "google"
|
@@ -60,42 +62,59 @@ chunks = chunk_text(concise_text_string, chunk_size=450)
|
|
60 |
# Build the vectorsore
|
61 |
vectorstore = build_faiss_vectorstore(chunks)
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
@app.post("/chat")
|
64 |
async def chat(request: ChatRequest):
|
65 |
try:
|
66 |
if request.model_choice == "google":
|
67 |
client = genai.Client(api_key=google_api_key)
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# Retrieve relevant text
|
70 |
results = retrieve(request.message, vectorstore, top_k=5)
|
71 |
formatted_results = "\n\n".join(results)
|
72 |
|
73 |
-
rag_prompt = f"""You have access to the following relevant information retrieved based on the user's query:
|
74 |
|
75 |
{formatted_results}
|
76 |
|
77 |
Using the information above, answer the user's query as accurately as possible:
|
78 |
|
79 |
User's Query: {request.message}
|
80 |
-
"""
|
81 |
-
|
82 |
-
# # summarize chat history
|
83 |
-
# summary_thresh = 2
|
84 |
-
# if len(request.chat_history) > summary_thresh:
|
85 |
-
# summarize_prompt = f"""Please summarize the following chat history concisely, focusing on the key points and main topics discussed. Avoid
|
86 |
-
# unnecessary details and provide a clear, straightforward summary. {request.chat_history[:-summary_thresh]}""" # summarize everything except last k items
|
87 |
-
# summary_response = client.models.generate_content(
|
88 |
-
# model="gemini-2.0-flash",
|
89 |
-
# contents=summarize_prompt,
|
90 |
-
# config=GenerateContentConfig(
|
91 |
-
# system_instruction=["You are a helpful assistant who is an expert at summarization."]
|
92 |
-
# ),
|
93 |
-
# )
|
94 |
-
# request.chat_history = request.chat_history[-(summary_thresh+1):] # keep last k items
|
95 |
-
# request.chat_history.insert(0, {"role": "user", "parts": [{"text": f"Here is a summary of this conversation so far: {summary_response.text}"}]})
|
96 |
|
97 |
# remove the unfformatted user message
|
98 |
del request.chat_history[-1]
|
|
|
99 |
# add the user message with RAG data
|
100 |
request.chat_history.append({"role": "user", "parts": [{"text": rag_prompt}]})
|
101 |
|
|
|
2 |
from pydantic import BaseModel
|
3 |
from huggingface_hub import InferenceClient
|
4 |
import os
|
5 |
+
import textwrap
|
6 |
from google import genai
|
7 |
from google.genai.types import GenerateContentConfig
|
8 |
from datasets import load_dataset
|
|
|
46 |
You focus on buck bedding, terrain reading, and aggressive yet calculated mobile tactics. Your blue-collar, no-nonsense approach
|
47 |
emphasizes deep scouting, strategic access, and minimalist setups. Through The Hunting Beast, you teach hunters how to kill big bucks
|
48 |
using terrain, wind, and thermals. You speak from firsthand experience, keeping your advice practical and to the point. Provide detailed
|
49 |
+
yet concise responses, with a maximum of 150 words.
|
50 |
+
"""
|
51 |
temperature: float = 0.7
|
52 |
chat_history: List[Dict[str, Any]] = []
|
53 |
model_choice: str = "google"
|
|
|
62 |
# Build the vectorsore
|
63 |
vectorstore = build_faiss_vectorstore(chunks)
|
64 |
|
65 |
+
one_shot_example = textwrap.dedent(""" Here is an example of the style and tone of a response:
|
66 |
+
Query: How do big bucks use clear cuts for bedding?
|
67 |
+
Response: Yeah, a lot of guys think big bucks just bed right in the middle of a clear cut because it’s thick, but that’s not really how they use it. The
|
68 |
+
thick regrowth is great for food and cover, but those bucks still want an advantage. Most of the time, they’re bedding on the edges, right where the cut
|
69 |
+
meets older timber. They’ll set up with the wind at their back so they can smell anything sneaking up behind them, and they’re looking out into the open
|
70 |
+
woods, watching for danger""")
|
71 |
+
|
72 |
@app.post("/chat")
|
73 |
async def chat(request: ChatRequest):
|
74 |
try:
|
75 |
if request.model_choice == "google":
|
76 |
client = genai.Client(api_key=google_api_key)
|
77 |
|
78 |
+
# insert one shot example at beginning of chat
|
79 |
+
request.chat_history.insert(0, {
|
80 |
+
"role": "user",
|
81 |
+
"parts": [{"text": one_shot_example}]
|
82 |
+
})
|
83 |
+
|
84 |
+
# summarize chat history
|
85 |
+
summary_thresh = 2
|
86 |
+
if len(request.chat_history) > summary_thresh:
|
87 |
+
summarize_prompt = f"""Please summarize the following chat history concisely, focusing on the key points and main topics discussed. Avoid
|
88 |
+
unnecessary details and provide a clear, straightforward summary. {request.chat_history[:-summary_thresh]}""" # summarize everything except last k items
|
89 |
+
summary_response = client.models.generate_content(
|
90 |
+
model="gemini-2.0-flash",
|
91 |
+
contents=summarize_prompt,
|
92 |
+
config=GenerateContentConfig(
|
93 |
+
system_instruction=["You are a helpful assistant who is an expert at summarization."]
|
94 |
+
),
|
95 |
+
)
|
96 |
+
request.chat_history = request.chat_history[-(summary_thresh+1):] # keep last k items
|
97 |
+
request.chat_history.insert(1,
|
98 |
+
{"role": "user",
|
99 |
+
"parts": [{"text": f"Here is a summary of this conversation so far: {summary_response.text}"}]})
|
100 |
+
|
101 |
+
|
102 |
# Retrieve relevant text
|
103 |
results = retrieve(request.message, vectorstore, top_k=5)
|
104 |
formatted_results = "\n\n".join(results)
|
105 |
|
106 |
+
rag_prompt = textwrap.dedent(f"""You have access to the following relevant information retrieved based on the user's query:
|
107 |
|
108 |
{formatted_results}
|
109 |
|
110 |
Using the information above, answer the user's query as accurately as possible:
|
111 |
|
112 |
User's Query: {request.message}
|
113 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
# remove the unfformatted user message
|
116 |
del request.chat_history[-1]
|
117 |
+
|
118 |
# add the user message with RAG data
|
119 |
request.chat_history.append({"role": "user", "parts": [{"text": rag_prompt}]})
|
120 |
|