NovakTJ commited on
Commit
731bda8
·
1 Parent(s): 65b833a

debugging za history

Browse files
Files changed (2) hide show
  1. src/agent.py +26 -32
  2. test_agent.py +47 -1
src/agent.py CHANGED
@@ -62,61 +62,55 @@ def chat_with_turbot(message: str, history: List[List[str]]) -> str:
62
  """
63
  Main function to chat with TurBot.
64
  """
65
- # Convert history to the format expected by our LLM
66
- messages = []
 
 
 
 
 
 
 
 
 
 
67
  for human, ai in history:
68
  if human:
69
- messages.append(HumanMessage(content=human))
70
  if ai:
71
- messages.append(AIMessage(content=ai))
72
 
73
  # Add the current message
74
- messages.append(HumanMessage(content=message))
75
 
76
  # Check if we should search for travel information
77
  context = ""
78
  if should_search_travel_info(message):
79
  context = search_travel_packages(message)
80
-
81
- # Prepare the system message
82
- if context:
83
- system_message = f"""Ti si TurBot, digitalni asistent za turističku agenciju.
84
- Koristi sledeće informacije o putovanjima da odgovoriš na pitanje korisnika:
85
-
86
- {context}
87
-
88
- Odgovori na srpskom jeziku i budi koristan i prijateljski nastrojen."""
89
- else:
90
- system_message = """Ti si TurBot, digitalni asistent za turističku agenciju.
91
- Odgovori na srpskom jeziku i budi koristan i prijateljski nastrojen.
92
- Ako ne znaš odgovor na pitanje, preporuči korisniku da pita o putovanjima, letovima, hotelima ili destinacijama."""
93
-
94
- # Add system message to the beginning
95
- messages.insert(0, HumanMessage(content=system_message))
96
 
97
  try:
98
- # Convert messages to the format expected by InferenceClient
99
- messages_for_client = []
100
- for msg in messages:
101
- if isinstance(msg, HumanMessage):
102
- messages_for_client.append({"role": "user", "content": msg.content})
103
- elif isinstance(msg, AIMessage):
104
- messages_for_client.append({"role": "assistant", "content": msg.content})
105
 
106
  # Generate response
107
  response = ""
108
- for message in client.chat_completion(
109
  messages_for_client,
110
  stream=True,
111
  max_tokens=512,
112
  temperature=0.7,
113
  top_p=0.95,
114
  ):
115
- if hasattr(message, 'choices') and message.choices:
116
- token = message.choices[0].delta.content
117
  if token:
118
  response += token
119
 
120
- return response
121
  except Exception as e:
122
  return f"Izvinjavam se, došlo je do greške: {str(e)}"
 
62
  """
63
  Main function to chat with TurBot.
64
  """
65
+ # Prepare the system message first
66
+ system_message = """Ti si TurBot, digitalni asistent za turističku agenciju.
67
+ Odgovori na srpskom jeziku i budi koristan i prijateljski nastrojen.
68
+ Ako ne znaš odgovor na pitanje, preporuči korisniku da pita o putovanjima, letovima, hotelima ili destinacijama."""
69
+
70
+ # Convert messages to the format expected by InferenceClient
71
+ messages_for_client = []
72
+
73
+ # Add system message first (only once)
74
+ messages_for_client.append({"role": "system", "content": system_message})
75
+
76
+ # Add conversation history
77
  for human, ai in history:
78
  if human:
79
+ messages_for_client.append({"role": "user", "content": human})
80
  if ai:
81
+ messages_for_client.append({"role": "assistant", "content": ai})
82
 
83
  # Add the current message
84
+ messages_for_client.append({"role": "user", "content": message})
85
 
86
  # Check if we should search for travel information
87
  context = ""
88
  if should_search_travel_info(message):
89
  context = search_travel_packages(message)
90
+ if context:
91
+ # Add context as additional information
92
+ messages_for_client.append({"role": "user", "content": f"Kontekst o putovanjima: {context}"})
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  try:
95
+ # Debug: Print messages being sent to model
96
+ print(f"\n🔍 DEBUG: Sending {len(messages_for_client)} messages to model:")
97
+ for i, msg in enumerate(messages_for_client):
98
+ print(f" {i+1}. {msg['role']}: {msg['content'][:100]}...")
 
 
 
99
 
100
  # Generate response
101
  response = ""
102
+ for message_chunk in client.chat_completion(
103
  messages_for_client,
104
  stream=True,
105
  max_tokens=512,
106
  temperature=0.7,
107
  top_p=0.95,
108
  ):
109
+ if hasattr(message_chunk, 'choices') and message_chunk.choices:
110
+ token = message_chunk.choices[0].delta.content
111
  if token:
112
  response += token
113
 
114
+ return response if response else "Izvinjavam se, nisam dobio odgovor od modela. Pokušajte ponovo."
115
  except Exception as e:
116
  return f"Izvinjavam se, došlo je do greške: {str(e)}"
test_agent.py CHANGED
@@ -1,10 +1,48 @@
1
  #!/usr/bin/env python3
2
  """
3
- Simple test script for TurBot agent.
4
  """
5
 
6
  from src.agent import chat_with_turbot
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def test_turbot():
9
  """Test the TurBot agent with some sample questions."""
10
 
@@ -37,4 +75,12 @@ def test_turbot():
37
  print("-" * 40)
38
 
39
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
40
  test_turbot()
 
1
  #!/usr/bin/env python3
2
  """
3
+ Simple test script for TurBot agent with memory debugging.
4
  """
5
 
6
  from src.agent import chat_with_turbot
7
 
8
+ def test_memory():
9
+ """Test the TurBot agent's memory with name test."""
10
+
11
+ print("🧠 Testing TurBot Memory - Name Test")
12
+ print("=" * 60)
13
+
14
+ test_questions = [
15
+ "Zdravo! Moje ime je Marko.",
16
+ "Kako se zovem?",
17
+ "Da li se sećaš mog imena?",
18
+ "Koje je moje ime?"
19
+ ]
20
+
21
+ history = []
22
+
23
+ for i, question in enumerate(test_questions):
24
+ print(f"\n👤 Korisnik: {question}")
25
+ print("-" * 40)
26
+
27
+ try:
28
+ response = chat_with_turbot(question, history)
29
+ print(f"🤖 TurBot: {response}")
30
+
31
+ # Add to history for next iteration
32
+ history.append([question, response])
33
+
34
+ # Debug: Show current history
35
+ print(f"\n📝 History length: {len(history)}")
36
+ print("📝 Current history:")
37
+ for j, (h, a) in enumerate(history):
38
+ print(f" {j+1}. User: {h[:50]}...")
39
+ print(f" Bot: {a[:50]}...")
40
+
41
+ except Exception as e:
42
+ print(f"❌ Greška: {str(e)}")
43
+
44
+ print("-" * 40)
45
+
46
  def test_turbot():
47
  """Test the TurBot agent with some sample questions."""
48
 
 
75
  print("-" * 40)
76
 
77
  if __name__ == "__main__":
78
+ # Run memory test first
79
+ test_memory()
80
+
81
+ print("\n" + "="*60)
82
+ print("Now testing regular questions...")
83
+ print("="*60)
84
+
85
+ # Then run regular test
86
  test_turbot()