-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat2.py
More file actions
75 lines (56 loc) · 2.17 KB
/
chat2.py
File metadata and controls
75 lines (56 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# openai basic chatbot with rolling 5-chat history
from openai import OpenAI
from rich.console import Console
from rich.markdown import Markdown
import os
import file_helper as fh
# initialize OpenRouter client
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.environ.get("OPENROUTER_API_KEY"),
default_headers={
"HTTP-Referer": "http://localhost",
"X-Title": "Basic OpenRouter ChatBot",
}
)
# initialize rich Console
console = Console()
# system message (always at index 0)
system_msg = {"role": "system", "content": "You are a Harvard CS50 teaching assistant for David Malan the Instructor. You are able to help me with any Computer Science related questions."}
# initialize messages list with system message
messages = [system_msg]
# maximum number of user-assistant pairs to keep
MAX_PAIRS = 5
print("Chat with rolling 5-chat history. Type 'quit' or 'exit' to end.")
while True:
# get user input
user_input = input("\nYou: ")
# check for exit condition
if user_input.lower() in ('quit', 'exit'):
print("Goodbye!")
break
# append user message to messages
messages.append({"role": "user", "content": user_input})
# make the API call with current messages
response = client.chat.completions.create(
model="qwen/qwen3-coder",
messages=messages
)
# extract the raw markdown text
raw_text = response.choices[0].message.content
# append assistant response to messages
messages.append({"role": "assistant", "content": raw_text})
# enforce rolling history: keep only MAX_PAIRS user-assistant pairs
# total messages = system (1) + pairs * 2
current_pairs = (len(messages) - 1) // 2
print(f"[Debug] pairs: {current_pairs}, total messages: {len(messages)}")
if current_pairs > MAX_PAIRS:
# remove oldest pair (messages[1] = first user, messages[2] = first assistant)
# delete both at once using slice assignment
messages[1:3] = []
print(f"[Debug] removed oldest pair, new pairs: {current_pairs-1}")
# render with Rich
md = Markdown(raw_text)
console.print(md)
# log response
fh.log_response(raw_text)