Untitled

 avatar
unknown
plain_text
2 months ago
1.4 kB
11
Indexable
from groq import Groq

client = Groq(api_key="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")

def add_to_chat_history(history, role, content):
	"""Append one message to chat history."""
	history.append({"role": role, "content": content})
	return history


def trigger_model(history, model="qwen/qwen3-32b", temperature=0.1):
	"""Send full chat history to the model and return assistant text."""
	completion = client.chat.completions.create(
		model=model,
		messages=history,
		temperature=temperature,
	)
	return completion.choices[0].message.content

  
def chat_interface():
	"""Simple CLI chat loop using in-memory history."""
	history = [
		{
			"role": "system",
			"content": "You are a helpful assistant. Keep replies short and clear.",
		}
	]

	print("Simple Groq Chat")
	print("Type 'exit' to quit.\n")

	while True:
		user_text = input("You: ").strip()
		if not user_text:
			continue
		if user_text.lower() in {"exit", "quit"}:
			print("Chat ended.")
			break
		add_to_chat_history(history, "user", user_text)

		try:
			assistant_text = trigger_model(history)
			
		except Exception as exc:
			print(f"Assistant: Request failed: {exc}")
			continue

		add_to_chat_history(history, "assistant", assistant_text)
		print(f"Assistant: {assistant_text}\n")


if __name__ == "__main__":
	chat_interface()
Editor is loading...
Leave a Comment