xdn666
(xdn666)
1
import anthropic
设置Claude API密钥
client = anthropic.Anthropic(
api_key=“******”,
base_url=“https://api.oaipro.com”
)
message = client.messages.create(
model=“claude-3-5-sonnet-20241022”,
max_tokens=1024,
messages=[
{“role”: “user”, “content”: “Hello, Claude”}
]
)
print(message.content)
为什么这个运行会报错啊,错误如下:
Error code: 404 - {‘error’: {‘message’: ‘Invalid URL (POST /v1/messages)’, ‘type’: ‘invalid_request_error’, ‘param’: ‘’, ‘code’: ‘’}}
3 个赞
xdn666
(xdn666)
7
但是又出现一个新问题,代码如下:
import openai
openai.api_key = ‘****’
openai.api_base = “https://api.oaipro.com/v1”
实现接口函数
def get_completion(prompt, model=“claude-3-5-sonnet-20241022”, temperature=1):
messages = [{“role”: “user”, “content”: prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
)
return response.choices[0].message[“content”]
prompt = “Hi, claude”
response = get_completion(prompt)
print(response)
程序报错:
无效的令牌 (request id:****)
xdn666
(xdn666)
11
找到原因了,额度不能设置为无限或者超出额度的key。。。。
3 个赞