File size: 1,971 Bytes
4514838 |
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 |
from groq import Groq
GROQ_API_KEY = "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"
def chat_with_groq(user_message):
"""Send a message to Groq and get the streaming response"""
client = Groq(api_key=GROQ_API_KEY)
try:
completion = client.chat.completions.create(
model="qwen/qwen3-32b",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": user_message}
],
temperature=0.6,
max_completion_tokens=4096,
top_p=0.95,
reasoning_effort="default",
stream=True,
stop=None,
)
print("Groq: ", end="", flush=True)
response_text = ""
for chunk in completion:
content = chunk.choices[0].delta.content or ""
print(content, end="", flush=True)
response_text += content
print() # New line after response
return response_text
except Exception as e:
return f"Error: {e}"
def main():
"""Main interactive loop"""
print("=== Groq Chat Bot ===")
print("Enter your questions, type 'quit' or 'exit' to quit")
print("=" * 30)
while True:
try:
user_input = input("\nYour question: ").strip()
if user_input.lower() in ['quit', 'exit', 'q']:
print("Goodbye!")
break
if not user_input:
print("Please enter a question.")
continue
print("\nThinking...")
chat_with_groq(user_input)
except KeyboardInterrupt:
print("\n\nProgram interrupted, goodbye!")
break
except EOFError:
print("\n\nInput ended, goodbye!")
break
if __name__ == "__main__":
main()
|