Skip to content

Commit

Permalink
add human readable result when there is not LLM studio working
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekleon committed Dec 4, 2024
1 parent 3552221 commit 85891d2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
11 changes: 11 additions & 0 deletions backend/app/answer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import Any
from typing_extensions import TypedDict
from app.models.event_model import Event

class Answer(TypedDict):
events: list[Event]
weather: str
pois: list[dict[str, Any]]
unesco_sites: list[str]
hotels_motels: list[str]
historic_places: list[str]
14 changes: 10 additions & 4 deletions backend/app/api/lmstudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import json

from app.config import LMSTUDIO_HOST, LMSTUDIO_ENDPOINT, LMSTUDIO_MODEL
from app.query_str import generate_ai_style_response
from app.answer import Answer

class Message(TypedDict):
role: Literal['user', 'assistant', 'system']
content: str

def lm_studio_request(messages: list[Message]) -> str:
def lm_studio_request(message: Answer) -> str:
full_url = f"http://{LMSTUDIO_HOST}/{LMSTUDIO_ENDPOINT}"

messages = [
{ "role": "system", "content": "You are application assistant. Based on given JSON tell what person can visit. Answer in human way like chat assistant talking to a person." },
{ "role": "user", "content": str(message) }
]

payload = {
"model": LMSTUDIO_MODEL,
Expand All @@ -32,6 +39,5 @@ def lm_studio_request(messages: list[Message]) -> str:
if response.status_code == 200:
json_repsonse = response.json()
return json_repsonse['choices'][0]['message']['content']
except: # noqa: E722
return str(messages[1])
return str(messages[1])
finally: # noqa: E722
return generate_ai_style_response(message["events"], message["weather"])
17 changes: 7 additions & 10 deletions backend/app/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
add_message,
create_new_chat
)
from app.answer import Answer

class QueryRequest(BaseModel):
user_input: str
Expand Down Expand Up @@ -74,18 +75,17 @@ def run_query(query: QueryRequest):
city=city,
)
weather_info = query_weather(weather_query)

if city and city.strip():
unesco_sites = localdatasets.get_unesco_sites(city)
hotels_motels = localdatasets.get_hotels_motels(city)
historic_places = localdatasets.get_historical_places(city)

unesco_sites = localdatasets.get_unesco_sites(city)
hotels_motels = localdatasets.get_hotels_motels(city)
historic_places = localdatasets.get_historical_places(city)

# TODO Implement ammenities keywords extraction in nlp
amenity = keywords[0] if keywords else "cafe" # Use first keyword as amenity type or default to "cafe"
poi_results = overpass.get_poi_data(city, amenity)

# Step 6: Return the compiled response
answer = {
answer: Answer = {
"events": event_results,
"weather": weather_info,
"pois": poi_results,
Expand All @@ -95,10 +95,7 @@ def run_query(query: QueryRequest):
}

# Step 7: Post-process with LLM to get more human-like response
ans = lm_studio_request([
{ "role": "system", "content": "You are application assistant. Based on given JSON tell what person can visit. Answer in human way like chat assistant talking to a person." },
{ "role": "user", "content": str(answer) }
])
ans = lm_studio_request(answer)

add_message(
InsertMessage(
Expand Down
27 changes: 27 additions & 0 deletions backend/app/query_str.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from datetime import datetime

from app.models.event_model import Event

def generate_ai_style_response(events: list[Event], weather: str) -> str:
# Sort events by priority and pinned status
sorted_events = sorted(events, key=lambda x: (-x.pinned, -x.priority, x.date or datetime.max))

# Start the response with the weather
response = f"The weather for the day is expected to be: {weather}.\n\n"

# Check if there are any events
if not sorted_events:
response += "It seems like there are no events scheduled for now. Maybe take the opportunity to relax or explore something spontaneous!"
else:
response += "Here are some events you might enjoy:\n"
for event in sorted_events:
event_date = (
f" on {event.date.strftime('%A, %B %d at %I:%M %p')}" if event.date else ""
)
description = ""

if event.description:
description = f" - {description}"

response += f"- **{event.name}** at {event.location}{event_date}{description}\n"
return response.strip()

0 comments on commit 85891d2

Please sign in to comment.