最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - groq.GroqError: The api_key client option must be set either by passing api_key to the client or by setting the GROQ_AP

programmeradmin2浏览0评论

I have been trying to use a LLama API usig Groq cloud but encountering this error. I have tried set GROQ_API_KEY=api_key

And $env:GROQ_API_KEY = "api_key"

But havent found the any solution and encountering the same error as the title.

CODE:

from groq import Groq
from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

# Retrieve the API key from the environment variable
api_key = os.getenv("MY_KEY")

# Initialize the Groq client with the API key
client = Groq(api_key=api_key)

# Prompt the user for input
user_input = input("Enter your message: ")

# Define the message list with the user's input
messages = [
    {"role": "user", "content": user_input}
]

# Create a chat completion request
completion = client.chatpletions.create(
    model="llama-3.3-70b-versatile",
    messages=messages,
    temperature=1,
    max_completion_tokens=1310,
    top_p=1,
    stream=True,
    stop=None,
)

# Stream and print the response
for chunk in completion:
    print(chunk.choices[0].delta.content or "", end="")

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论