fix chat issues
This commit is contained in:
@@ -70,9 +70,7 @@ async def log_requests(request: Request, call_next):
|
||||
logger.info(log_msg)
|
||||
print(log_msg)
|
||||
|
||||
headers_msg = f"🔥 Headers: {dict(request.headers)}"
|
||||
logger.info(headers_msg)
|
||||
print(headers_msg)
|
||||
|
||||
|
||||
# Get request body for POST requests
|
||||
if request.method == "POST":
|
||||
|
||||
+46
-6
@@ -62,6 +62,8 @@ def setup_prompt_template(theme: int, resume: str,full_history=None,form_respons
|
||||
("system", chat_prompt(theme, resume,full_history,form_response,generate_theme)),
|
||||
MessagesPlaceholder(variable_name="messages")
|
||||
])
|
||||
|
||||
|
||||
def fetch_conversation_history(conversation_id: str) -> List[Message]:
|
||||
"""
|
||||
Fetch conversation history from the API using the conversation ID.
|
||||
@@ -74,18 +76,54 @@ def fetch_conversation_history(conversation_id: str) -> List[Message]:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status() # Raise an error for bad responses
|
||||
data = response.json()["data"] # First JSON parse
|
||||
data = json.loads(json.loads(data))
|
||||
|
||||
if isinstance(data, str):
|
||||
print("Data is a string, parsing as JSON...")
|
||||
data = json.loads(data)
|
||||
|
||||
|
||||
# Parse the API response into Message objects
|
||||
messages = []
|
||||
for item in data:
|
||||
role = item.get("role", "unknown")
|
||||
content = item.get("content", "")
|
||||
timestamp = datetime.now().isoformat() # Use current timestamp if not provided
|
||||
messages.append(Message(role=role, content=content))
|
||||
|
||||
# Check if data exists and is a list
|
||||
if data and isinstance(data, list):
|
||||
for item in data:
|
||||
|
||||
|
||||
# Check if item is a dictionary
|
||||
if isinstance(item, dict):
|
||||
role = item.get("role", "unknown")
|
||||
content = item.get("content", "")
|
||||
timestamp = datetime.now().isoformat() # Use current timestamp if not provided
|
||||
messages.append(Message(role=role, content=content))
|
||||
elif isinstance(item, str):
|
||||
# If item is a string, it might be JSON that needs parsing
|
||||
try:
|
||||
parsed_item = json.loads(item)
|
||||
if isinstance(parsed_item, dict):
|
||||
role = parsed_item.get("role", "unknown")
|
||||
content = parsed_item.get("content", "")
|
||||
messages.append(Message(role=role, content=content))
|
||||
else:
|
||||
print(f"Parsed item is not a dict: {parsed_item}")
|
||||
except json.JSONDecodeError as json_err:
|
||||
print(f"Failed to parse JSON string: {item}, error: {json_err}")
|
||||
else:
|
||||
print(f"Unexpected item type: {type(item)} for item: {item}")
|
||||
else:
|
||||
print(f"No data or data is not a list. Data: {data}")
|
||||
|
||||
return messages
|
||||
|
||||
except requests.RequestException as e:
|
||||
print(f"Error fetching conversation history: {e}")
|
||||
return []
|
||||
except KeyError as e:
|
||||
print(f"Expected key not found in response: {e}")
|
||||
return []
|
||||
except Exception as e:
|
||||
print(f"Unexpected error: {e}")
|
||||
return []
|
||||
|
||||
|
||||
def convert_to_langchain_messages(messages: List[Message]) -> List[HumanMessage | AIMessage]:
|
||||
@@ -130,6 +168,8 @@ def ai_chat(query: str, conversation_id: str, theme_id: int, resume: str, full_h
|
||||
|
||||
# Fetch conversation history from the API
|
||||
history = fetch_conversation_history(conversation_id)
|
||||
|
||||
print(history)
|
||||
|
||||
config = {"configurable": {"thread_id": conversation_id}}
|
||||
language = "English"
|
||||
|
||||
Reference in New Issue
Block a user