-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_history.py
More file actions
37 lines (28 loc) · 962 Bytes
/
openai_history.py
File metadata and controls
37 lines (28 loc) · 962 Bytes
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
from openai import OpenAI
# Configure OpenAI client to use the local server
API_BASE = "http://127.0.0.1:1234/v1"
API_KEY = "not-needed" # Placeholder, required even if not used
MODEL = "hermes-3-llama-3.2-3b"
openai = OpenAI(base_url=API_BASE, api_key=API_KEY)
# Initialize chat history with system prompt
messages = [
{"role": "system", "content": "You are a helpful assistant."}
]
print("Chat with your local LLM. Type 'exit' to quit.")
while True:
user_input = input("You: ")
if user_input.strip() in {"exit", "quit"}:
break
messages.append({"role": "user", "content": user_input})
try:
response = openai.chat.completions.create(
model=MODEL,
messages=messages,
temperature=0.2
)
reply = response.choices[0].message.content
print(reply)
messages.append({"role": "assistant", "content": reply})
except Exception as e:
print(f"Error: {e}"
)