Spaces:
Sleeping
Sleeping
File size: 1,400 Bytes
d36aa50 5d50da3 89964de d36aa50 |
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 |
import os
from groq import Groq
# Retrieve the DEEPSEEK_API_KEY from environment variables or secret manager
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY") # Assumes the key is stored as an environment variable
# Ensure the API key is available
if not DEEPSEEK_API_KEY:
raise ValueError("API key is missing. Please check your environment variables.")
# Initialize the Groq client with the retrieved API key
client = Groq(api_key=DEEPSEEK_API_KEY)
# Prepare the message (this is the input from the user)
messages = [{"role": "user", "content": "Hello, how can I improve my coding skills?"}]
# Call the API to get a completion from DeepSeek (streaming)
try:
print("Calling API...")
# Make the API call
completion = client.chat.completions.create(
model="meta-llama/llama-4-scout-17b-16e-instruct", # Model to use
messages=messages, # Messages to send
temperature=1, # Adjust the creativity of the response
max_completion_tokens=1024, # Max tokens for the completion
top_p=1, # Top-p sampling for response variety
stream=True # Enable streaming mode
)
print("Streaming response received...")
# Handle the streaming response (output each chunk as it arrives)
for chunk in completion:
print(chunk.choices[0].delta.content or "", end="")
except Exception as e:
print(f"Error occurred: {str(e)}")
|