大家好,我是淘小白~

先把实用干货放在最前面!

Claude api说明文档网址:直达

Claude GitHub PythonSDK网址:直达

一、api价格 

api模型价格说明图片 

二、Python调用api方法代码:

Usage

from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT

anthropic = Anthropic(
    # defaults to os.environ.get("ANTHROPIC_API_KEY")
    api_key="my api key",
)

completion = anthropic.completions.create(
    model="claude-2",
    max_tokens_to_sample=300,
    prompt=f"{HUMAN_PROMPT} how does a court case get to the Supreme Court? {AI_PROMPT}",
)
print(completion.completion)

 Async Usage

from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT

anthropic = AsyncAnthropic(
    # defaults to os.environ.get("ANTHROPIC_API_KEY")
    api_key="my api key",
)


async def main():
    completion = await anthropic.completions.create(
        model="claude-2",
        max_tokens_to_sample=300,
        prompt=f"{HUMAN_PROMPT} how does a court case get to the Supreme Court? {AI_PROMPT}",
    )
    print(completion.completion)


asyncio.run(main())

Streaming Responses

from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT

anthropic = Anthropic()

stream = anthropic.completions.create(
    prompt=f"{HUMAN_PROMPT} Your prompt here {AI_PROMPT}",
    max_tokens_to_sample=300,
    model="claude-2",
    stream=True,
)
for completion in stream:
    print(completion.completion)
from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT

anthropic = AsyncAnthropic()

stream = await anthropic.completions.create(
    prompt=f"{HUMAN_PROMPT} Your prompt here {AI_PROMPT}",
    max_tokens_to_sample=300,
    model="claude-2",
    stream=True,
)
async for completion in stream:
    print(completion.completion)

上面的数据流是一个字一个字输出的,类似打字机的效果,Python调用的话,我修改了一下可以看下下面的方法代码 :

from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT

def main(content):
    anthropic = Anthropic(api_key="----------------")

    stream = anthropic.completions.create(
        prompt=f"{HUMAN_PROMPT} {content} {AI_PROMPT}",
        max_tokens_to_sample=30000,
        model="claude-2",
        stream=True,
    )
    
    for completion in stream:
        print(completion.completion)
   

需要apikey ,这个某宝上可以买到,自己去买就可以了几百块一个 

三、错误代码 

状态码 错误类型
400 BadRequestError
401 AuthenticationError
403 PermissionDeniedError
404 NotFoundError
第422章 UnprocessableEntityError
第429章 RateLimitError
>=500 InternalServerError
不适用 APIConnectionError

四、测试经验

1、api接口和网页版的数据结果有差距,网页版的更好;

2、api接口根据原文改写文章,字数可以拓展,字数有多有少;

3、接口有道德约束,遇到道德约束时,生成的文章很少;

4、根据提纲写文章,字数基本都是在1000字以内;

5、指令控制不住ai,很多要求做不到;❤ TXB2196

6、改写的文章,有的可以通过ai原创检测,其次,如果用口语化的指令改写文章过ai检测的效果更好,但是不是所有的改写都可以通过ai原创检测;

7、价格比gpt低,输入的内容更多,目前通过某宝购买的apikey,没有做过多的限制,可以无限量使用,一个key可以用很久,但是并发只能是1。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐