add gradion-hitl example

This commit is contained in:
Wetter
2025-05-30 16:55:33 +00:00
parent d13180f835
commit ecbb7e6e4d
12 changed files with 570 additions and 0 deletions
@@ -0,0 +1,21 @@
import os
from openai import OpenAI
from openai.types.chat.chat_completion import ChatCompletion
api_key = os.getenv("OPENAI_API_KEY")
base_url = "https://api.openai.com/v1"
model = "gpt-4o"
def call_llm(message: str):
print(f"Calling LLM with message: \n{message}")
client = OpenAI(api_key=api_key, base_url=base_url)
response: ChatCompletion = client.chat.completions.create(
model=model, messages=[{"role": "user", "content": message}]
)
return response.choices[0].message.content
if __name__ == "__main__":
print(call_llm("Hello, how are you?"))
@@ -0,0 +1,39 @@
import random
from datetime import date, datetime
def call_check_weather_api(city: str, date: date | None):
if date is None:
date = datetime.now().date()
current_date = datetime.now().date()
# calculate date difference
date_diff = (date - current_date).days
# check if the date is within the allowed range
if abs(date_diff) > 7:
return f"Failed to check weather: Date {date} is more than 7 days away from current date."
return f"The weather in {city} on {date} is {random.choice(['sunny', 'cloudy', 'rainy', 'snowy'])}, and the temperature is {random.randint(10, 30)}°C."
def call_book_hotel_api(hotel: str, checkin_date: date, checkout_date: date):
current_date = datetime.now().date()
# check if the checkin date is after the current date
if checkin_date <= current_date:
return (
f"Failed to book hotel {hotel}: Check-in date must be after current date."
)
# check if the checkin date is before the checkout date
if checkin_date >= checkout_date:
return f"Failed to book hotel {hotel}, because the checkin date is after the checkout date."
# check if the date difference is more than 7 days
date_diff = (checkout_date - checkin_date).days
if date_diff > 7:
return f"Failed to book hotel {hotel}: Stay duration cannot exceed 7 days."
return f"Booked hotel {hotel} from {checkin_date.strftime('%Y-%m-%d')} to {checkout_date.strftime('%Y-%m-%d')} successfully."
@@ -0,0 +1,11 @@
conversation_cache = {}
def load_conversation(conversation_id: str):
print(f"Loading conversation {conversation_id}")
return conversation_cache.get(conversation_id, {})
def save_conversation(conversation_id: str, session: dict):
print(f"Saving conversation {session}")
conversation_cache[conversation_id] = session
@@ -0,0 +1,28 @@
def format_chat_history(history):
"""
Format the chat history for LLM
Args:
history (list): The chat history list, each element contains role and content
Returns:
str: The formatted chat history string
"""
if not history:
return "No history"
formatted_history = []
for message in history:
role = "user" if message["role"] == "user" else "assistant"
content = message["content"]
# filter out the thinking content
if role == "assistant":
if (
content.startswith("- 🤔")
or content.startswith("- ➡️")
or content.startswith("- ⬅️")
):
continue
formatted_history.append(f"{role}: {content}")
return "\n".join(formatted_history)