|
| 1 | +from volcenginesdkarkruntime import Ark |
| 2 | + |
| 3 | +# Authentication |
| 4 | +# 1.If you authorize your endpoint using an API key, you can set your api key to environment variable "ARK_API_KEY" |
| 5 | +# or specify api key by Ark(api_key="${YOUR_API_KEY}"). |
| 6 | +# Note: If you use an API key, this API key will not be refreshed. |
| 7 | +# To prevent the API from expiring and failing after some time, choose an API key with no expiration date. |
| 8 | + |
| 9 | +# 2.If you authorize your endpoint with Volcengine Identity and Access Management(IAM), |
| 10 | +# set your api key to environment variable "VOLC_ACCESSKEY", "VOLC_SECRETKEY" |
| 11 | +# or specify ak&sk by Ark(ak="${YOUR_AK}", sk="${YOUR_SK}"). |
| 12 | +# To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568) |
| 13 | +# For more information,please check this document(https://www.volcengine.com/docs/82379/1263279) |
| 14 | +client = Ark() |
| 15 | + |
| 16 | +if __name__ == "__main__": |
| 17 | + # Non-streaming: |
| 18 | + print("----- standard request -----") |
| 19 | + completion = client.chat.completions.create( |
| 20 | + model="${YOUR_ENDPOINT_ID}", |
| 21 | + messages=[ |
| 22 | + {"role": "user", "content": "How many Rs are there in the word 'strawberry'?"}, |
| 23 | + ], |
| 24 | + ) |
| 25 | + print(completion.choices[0].message.reasoning_content) |
| 26 | + print(completion.choices[0].message.content) |
| 27 | + |
| 28 | + # Streaming: |
| 29 | + print("----- streaming request -----") |
| 30 | + stream = client.chat.completions.create( |
| 31 | + model="${YOUR_ENDPOINT_ID}", |
| 32 | + messages=[ |
| 33 | + {"role": "user", "content": "How many Rs are there in the word 'strawberry'?"}, |
| 34 | + ], |
| 35 | + stream=True |
| 36 | + ) |
| 37 | + for chunk in stream: |
| 38 | + if not chunk.choices: |
| 39 | + continue |
| 40 | + if chunk.choices[0].delta.reasoning_content: |
| 41 | + print(chunk.choices[0].delta.reasoning_content, end="") |
| 42 | + else: |
| 43 | + print(chunk.choices[0].delta.content, end="") |
| 44 | + print() |
0 commit comments