-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathmain.py
More file actions
155 lines (128 loc) · 4.68 KB
/
main.py
File metadata and controls
155 lines (128 loc) · 4.68 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# app.py
import os
import streamlit as st
from typing import List, Dict, Optional
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
# ---------- Page setup ----------
st.set_page_config(page_title="Mini-ChatGPT", page_icon="💬")
st.title("💬 Mini-ChatGPT")
# ---------- Helpers ----------
def get_token() -> Optional[str]:
# 1) Session token set via sidebar
token = st.session_state.get("gh_token_input") or None
# 2) Secrets
if not token:
try:
token = st.secrets.get("GITHUB_TOKEN", None) # type: ignore[attr-defined]
except Exception:
token = None
# 3) Environment var
if not token:
token = os.getenv("GITHUB_TOKEN")
return token or None
def build_messages(system_prompt: str, history: List[Dict[str, str]], user_text: str):
msgs = []
if system_prompt.strip():
msgs.append(SystemMessage(content=system_prompt.strip()))
for turn in history:
role, content = turn["role"], turn["content"]
if role == "user":
msgs.append(HumanMessage(content=content))
elif role == "assistant":
msgs.append(AIMessage(content=content))
msgs.append(HumanMessage(content=user_text))
return msgs
def get_model(endpoint: str, model_name: str, credential: str):
# Keep the constructor simple/portable; advanced params can be added if needed.
return AzureAIChatCompletionsModel(
endpoint=endpoint,
model=model_name,
credential=credential,
)
# ---------- Sidebar: Settings ----------
with st.sidebar:
st.header("Settings")
# Token input (masked). Stored in session_state only.
st.text_input(
"GitHub Token",
key="gh_token_input",
type="password",
help=(
"A GitHub token with access to GitHub Models. "
"Recommended scopes: `models:read` (and any required by your setup). "
"For deployments, put it in `st.secrets` as GITHUB_TOKEN."
),
)
st.caption("We keep your token only in this browser session and never log it.")
endpoint = st.text_input(
"Endpoint",
value="https://models.github.ai/inference",
help="Default endpoint for GitHub Models.",
)
model_name = st.selectbox(
"Model",
options=[
"openai/gpt-4.1",
"openai/gpt-4.1-mini",
"openai/gpt-4o",
"openai/gpt-4o-mini",
],
index=0,
help="Pick a chat-capable model.",
)
system_prompt = st.text_area(
"System Prompt (optional)",
value="You are a concise, helpful assistant.",
height=100,
help="Sets behavior/tone for the assistant.",
)
col_a, col_b = st.columns(2)
with col_a:
if st.button("🧹 Clear chat"):
st.session_state["chat_history"] = []
st.rerun()
with col_b:
st.write("") # spacer
# ---------- Session state ----------
if "chat_history" not in st.session_state:
st.session_state[
"chat_history"
] = [] # list[{"role": "user"|"assistant", "content": str}]
# ---------- Render existing chat ----------
for msg in st.session_state.chat_history:
with st.chat_message("user" if msg["role"] == "user" else "assistant"):
st.markdown(msg["content"])
# ---------- Chat input ----------
user_text = st.chat_input("Type your message…")
if user_text:
token = get_token()
if not token:
st.error(
"Missing GitHub token. Add it in the sidebar, set `GITHUB_TOKEN` in environment, "
"or provide it via `st.secrets`."
)
else:
# Echo user's message in the UI and store it
st.session_state.chat_history.append({"role": "user", "content": user_text})
with st.chat_message("user"):
st.markdown(user_text)
# Call the model
try:
model = get_model(
endpoint=endpoint, model_name=model_name, credential=token
)
msgs = build_messages(
system_prompt, st.session_state.chat_history[:-1], user_text
)
with st.chat_message("assistant"):
with st.spinner("Thinking…"):
response = model.invoke(msgs)
# LangChain returns an AIMessage; content holds the text
assistant_text = getattr(response, "content", str(response))
st.markdown(assistant_text)
st.session_state.chat_history.append(
{"role": "assistant", "content": assistant_text}
)
except Exception as e:
st.error(f"Request failed: {e}")