Source: https://aiprimetech.io/vi/docs/sdks/python/ [Trang chủ](/vi/) / [Tài liệu](/vi/docs/) / Python # Python Các ví dụ hoạt động được với gói anthropic và openai, bao gồm truyền luồng và xử lý bất đồng bộ. Sao chép trang [ Xem dưới dạng Markdown](/vi/docs/sdks/python.md) [ Hỏi ChatGPT](https://chatgpt.com/?q=Read%20https%3A//aiprimetech.io/docs/sdks/python.md%20and%20answer%20questions%20about%20it.%20Start%20with%20a%20short%20summary.) [ Hỏi trong Claude](https://claude.ai/new?q=Read%20https%3A//aiprimetech.io/docs/sdks/python.md%20and%20answer%20questions%20about%20it.%20Start%20with%20a%20short%20summary.) ## SDK Anthropic ``` Copypip install anthropic ``` ``` Copyimport os from anthropic import Anthropic client = Anthropic( api_key=os.environ[class="s">"CLAUDEAPIKEY"], base_url=class="s">"https://aiprimetech.io", class=class="s">"c"># no /v1 ) msg = client.messages.create( model=class="s">"claude-sonnet-4-6", max_tokens=1024, system=class="s">"You are a concise assistant.", messages=[{class="s">"role": class="s">"user", class="s">"content": class="s">"Explain HTTP caching in three sentences."}], ) print(msg.content[0].text) print(msg.usage.input_tokens, msg.usage.output_tokens) ``` ## Truyền dữ liệu theo luồng ``` Copywith client.messages.stream( model=class="s">"claude-sonnet-4-6", max_tokens=1024, messages=[{class="s">"role": class="s">"user", class="s">"content": class="s">"Write a haiku about latency."}], ) as stream: for text in stream.text_stream: print(text, end=class="s">"", flush=True) final = stream.get_final_message() print(class="s">"\n", final.usage.output_tokens, class="s">"output tokens") ``` ## Bất đồng bộ ``` Copyimport asyncio from anthropic import AsyncAnthropic client = AsyncAnthropic(api_key=class="s">"sk-your-key", base_url=class="s">"https://aiprimetech.io") async def ask(q): m = await client.messages.create( model=class="s">"claude-haiku-4-5", max_tokens=256, messages=[{class="s">"role": class="s">"user", class="s">"content": q}], ) return m.content[0].text async def main(): sem = asyncio.Semaphore(8) class=class="s">"c"># respect rate limits async def guarded(q): async with sem: return await ask(q) print(await asyncio.gather(*(guarded(q) for q in [class="s">"1+1?", class="s">"2+2?", class="s">"3+3?"]))) asyncio.run(main()) ``` ## SDK OpenAI ``` Copyfrom openai import OpenAI client = OpenAI(api_key=class="s">"sk-your-key", base_url=class="s">"https://aiprimetech.io/v1") class=class="s">"c"># with /v1 resp = client.chat.completions.create( model=class="s">"claude-sonnet-4-6", messages=[{class="s">"role": class="s">"user", class="s">"content": class="s">"Hello"}], ) print(resp.choices[0].message.content) ``` Cả hai đoạn mã đều gọi cùng một mô hình với cùng mức giá. Hãy dùng thư viện mà dự án của bạn đã phụ thuộc. [API MessagesTài liệu tham chiếu các trường](/vi/docs/api-reference/messages/)[Truyền dữ liệu theo luồngCấu trúc sự kiện](/vi/docs/api-reference/streaming/)[Các phương pháp hay nhấtThời gian chờ và thử lại](/vi/docs/guides/best-practices/) [TrướcTổng quan về SDK](/vi/docs/sdks/)[Tiếp theoTypeScript](/vi/docs/sdks/typescript/)