### Python OpenAI SDK Example for iFlow API Source: https://platform.iflow.cn/docs/index This Python snippet demonstrates how to use the OpenAI SDK to interact with the iFlow API for chat completions. It requires the 'openai' library and your iFlow API key. ```python from openai import OpenAI client = OpenAI( base_url="https://apis.iflow.cn/v1", api_key="", ) completion = client.chat.completions.create( extra_body={}, model="TBStars2-200B-A13B", messages=[ { "role": "user", "content": "What is the meaning of life?" } ] ) print(completion.choices[0].message.content) ``` -------------------------------- ### TypeScript OpenAI SDK Example for iFlow API Source: https://platform.iflow.cn/docs/index This TypeScript snippet shows how to utilize the OpenAI SDK with the iFlow API for chat completions. Ensure you have the 'openai' package installed and replace the placeholder with your actual iFlow API key. ```typescript import OpenAI from 'openai'; const openai = new OpenAI({ baseURL: "https://apis.iflow.cn/v1", apiKey: "" }); async function main() { const completion = await openai.chat.completions.create({ model: "TBStars2-200B-A13B", messages: [ { "role": "user", "content": "What is the meaning of life?" } ], }); console.log(completion.choices[0].message); } main(); ``` -------------------------------- ### JavaScript Fetch Example for iFlow API Source: https://platform.iflow.cn/docs/index This JavaScript snippet utilizes the 'fetch' API to send a POST request to the iFlow chat completions endpoint. It includes setting the necessary headers and request body, then logs the content of the assistant's message. ```javascript const response = await fetch('https://apis.iflow.cn/v1/chat/completions', { method: 'POST', headers: { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json' }, body: JSON.stringify({ model: 'TBStars2-200B-A13B', messages: [ {role: 'user', content: '写一个快速排序算法的 Python 实现'} ], temperature: 0.7, max_tokens: 1000 }) }); const data = await response.json(); console.log(data.choices[0].message.content); ``` -------------------------------- ### Python Requests Example for iFlow API Source: https://platform.iflow.cn/docs/index This Python script uses the 'requests' library to send a POST request to the iFlow API for chat completions. It configures the API URL, headers, and payload, then prints the content of the assistant's message from the response. ```python import requests # 配置 API url = "https://apis.iflow.cn/v1/chat/completions" headers = { "Authorization": "Bearer YOUR_API_KEY", "Content-Type": "application/json" } # 请求数据 data = { "model": "TBStars2-200B-A13B", "messages": [ {"role": "user", "content": "解释一下量子计算的基本原理"} ], "temperature": 0.7, "max_tokens": 1000 } # 发起请求 response = requests.post(url, json=data, headers=headers) result = response.json() # 打印结果 print(result["choices"][0]["message"]["content"]) ``` -------------------------------- ### Create Text Chat Request (cURL) Source: https://platform.iflow.cn/docs/api-reference Example of making a POST request to the iFlow chat completions API using cURL. It includes setting the Authorization header and sending a JSON payload with chat messages and model parameters. ```bash curl -X POST \ https://apis.iflow.cn/v1/chat/completions \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer ' \ -d '{ \ "messages": [ \ { \ "role": "user", \ "content": "中国大模型行业2025年将会迎来哪些机遇和挑战?" \ } \ ], \ "model": "tstars2.0", \ "temperature": 0.7, \ "max_tokens": 512 \ }' ``` -------------------------------- ### cURL Command for iFlow API Chat Completions Source: https://platform.iflow.cn/docs/index This example demonstrates how to make a POST request to the iFlow API's chat completions endpoint using cURL. It includes setting the Content-Type and Authorization headers, along with the request body containing the model and messages. ```bash curl https://apis.iflow.cn/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "model": "TBStars2-200B-A13B", "messages": [ {"role": "system", "content": "你是一个专业的AI助手。"}, {"role": "user", "content": "请介绍一下人工智能的发展历史"} ], "temperature": 0.7, "max_tokens": 1000 }' ``` -------------------------------- ### Send POST Request to iFlow CN API (JavaScript) Source: https://platform.iflow.cn/docs/api-reference This JavaScript example utilizes the `fetch` API to send a POST request to the Platform iFlow CN API. It defines the request URL, constructs the JSON payload with model and message details, and sets the Authorization and Content-Type headers. ```javascript const url = "https://apis.iflow.cn/v1/chat/completions"; const payload = { "model": "tstars2.0", "messages": [ { "role": "user", "content": "中国大模型行业2025年将会迎来哪些机遇和挑战?" } ], "stream": false, "max_tokens": 512, "stop": ["null"], "temperature": 0.7, "top_p": 0.7, "top_k": 50, "frequency_penalty": 0.5, "n": 1, "response_format": {"type": "text"}, "tools": [ { "type": "function", "function": { "description": "", "name": "", "parameters": {}, "strict": false } } ] }; const headers = { "Authorization": "Bearer ", "Content-Type": "application/json" }; fetch(url, { method: "POST", headers: headers, body: JSON.stringify(payload) }) .then(response => response.json()) .then(data => console.log(data)); ``` -------------------------------- ### Send POST Request to iFlow CN API (Python) Source: https://platform.iflow.cn/docs/api-reference This Python example uses the `requests` library to send a POST request to the Platform iFlow CN API. It constructs the JSON payload with model parameters, user messages, and tool definitions, and includes the necessary Authorization and Content-Type headers. ```python import requests url = "https://apis.iflow.cn/v1/chat/completions" payload = { "model": "tstars2.0", "messages": [ { "role": "user", "content": "中国大模型行业2025年将会迎来哪些机遇和挑战?" } ], "stream": False, "max_tokens": 512, "stop": ["null"], "temperature": 0.7, "top_p": 0.7, "top_k": 50, "frequency_penalty": 0.5, "n": 1, "response_format": {"type": "text"}, "tools": [ { "type": "function", "function": { "description": "", "name": "", "parameters": {}, "strict": False } } ] } headers = { "Authorization": "Bearer ", "Content-Type": "application/json" } response = requests.request("POST", url, json=payload, headers=headers) print(response.text) ``` -------------------------------- ### Send POST Request to iFlow CN API (CURL) Source: https://platform.iflow.cn/docs/api-reference This CURL example demonstrates how to send a POST request to the Platform iFlow CN API for chat completions. It includes setting the Authorization header with an API key, specifying the content type, and providing a JSON payload with model details, messages, and tool configurations. ```curl curl --request POST \n --url https://apis.iflow.cn/v1/chat/completions \n --header 'Authorization: Bearer ' \n --header 'Content-Type: application/json' \n --data '{ \n "model": "tstars2.0", \n "messages": [ \n { \n "role": "user", \n "content": "中国大模型行业2025年将会迎来哪些机遇和挑战?" \n } \n ], \n "stream": false, \n "max_tokens": 512, \n "stop": [ \n "null" \n ], \n "temperature": 0.7, \n "top_p": 0.7, \n "top_k": 50, \n "frequency_penalty": 0.5, \n "n": 1, \n "response_format": { \n "type": "text" \n }, \n "tools": [ \n { \n "type": "function", \n "function": { \n "description": "", \n "name": "", \n "parameters": {}, \n "strict": false \n } \n } \n ] \n}' ``` -------------------------------- ### Send POST Request to iFlow CN API (Java) Source: https://platform.iflow.cn/docs/api-reference This Java example demonstrates how to send a POST request to the Platform iFlow CN API using Unirest. It constructs the request body as a JSON string using Jackson ObjectMapper, including model parameters, messages, and tool definitions, and sets the necessary headers. ```java import com.fasterxml.jackson.databind.ObjectMapper; import java.util.*; // 构建消息对象 Map message = new HashMap<>(); message.put("role", "user"); message.put("content", "中国大模型行业2025年将会迎来哪些机遇和挑战?"); // 构建工具函数对象 Map function = new HashMap<>(); function.put("description", ""); function.put("name", ""); function.put("parameters", new HashMap<>()); function.put("strict", false); Map tool = new HashMap<>(); tool.put("type", "function"); tool.put("function", function); // 构建响应格式对象 Map responseFormat = new HashMap<>(); responseFormat.put("type", "text"); // 构建请求参数 Map requestBody = new HashMap<>(); requestBody.put("model", "tstars2.0"); requestBody.put("messages", Arrays.asList(message)); requestBody.put("stream", false); requestBody.put("max_tokens", 512); requestBody.put("stop", Arrays.asList("null")); requestBody.put("temperature", 0.7); requestBody.put("top_p", 0.7); requestBody.put("top_k", 50); requestBody.put("frequency_penalty", 0.5); requestBody.put("n", 1); requestBody.put("response_format", responseFormat); requestBody.put("tools", Arrays.asList(tool)); // 转换为JSON字符串 ObjectMapper mapper = new ObjectMapper(); String jsonBody = mapper.writeValueAsString(requestBody); // 发送请求 HttpResponse response = Unirest.post("https://apis.iflow.cn/v1/chat/completions") .header("Authorization", "Bearer ") .header("Content-Type", "application/json") .body(jsonBody) .asString(); ``` -------------------------------- ### 聊天补全 API Source: https://platform.iflow.cn/docs/index 该 API 允许您与心流 AI 模型进行对话,支持流式和非流式响应。您可以通过提供模型名称和消息列表来获取 AI 的回复。 ```APIDOC ## POST /v1/chat/completions ### Description This endpoint allows you to interact with the Xinliu AI models for chat completions. It supports both streaming and non-streaming responses. ### Method POST ### Endpoint `/v1/chat/completions` ### Parameters #### Headers - **Authorization** (string) - Required - Your API key in the format `Bearer YOUR_API_KEY`. - **Content-Type** (string) - Required - `application/json` #### Request Body - **model** (string) - Required - The ID of the model to use for the completion (e.g., `TBStars2-200B-A13B`). - **messages** (array) - Required - A list of message objects, where each object has a `role` (e.g., `system`, `user`, `assistant`) and `content` (the message text). - **temperature** (number) - Optional - Controls the randomness of the output. Higher values mean more random. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate in the completion. ### Request Example ```json { "model": "TBStars2-200B-A13B", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is the weather like today?"} ], "temperature": 0.7, "max_tokens": 150 } ``` ### Response #### Success Response (200) - **choices** (array) - A list of message choices returned by the model. - **message** (object) - **role** (string) - The role of the author of the message (`assistant`). - **content** (string) - The content of the assistant's message. #### Response Example ```json { "id": "chatcmpl-12345", "object": "chat.completion", "created": 1677652288, "model": "TBStars2-200B-A13B", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "The weather today is sunny with a high of 75 degrees Fahrenheit." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30 } } ``` ``` -------------------------------- ### Chat Completion Response Parameters (Streaming) Source: https://platform.iflow.cn/docs/api-reference Describes the parameters returned in a streaming response from the Chat Completion API. Includes information about choices, usage, model, delta, and tool calls. ```APIDOC ## Chat Completion Response (Streaming) ### Description Details the structure of the JSON response when using a streaming request to the Chat Completion API. ### Parameters - `id` (string) - Required - The unique identifier of the chat completion. Each chunk has the same ID. - `choices` (object[]) - Required - A list of choices generated by the model. - `choices.finish_reason` (enum) - Optional - The reason the generation ended. Possible values: `stop`, `eos`, `length`, `tool_calls`. - `choices.message` (object) - Required - The message object returned by the model. - `choices.logprobs` (object | null) - Optional - Log probability information for the option. - `choices.logprobs.content` (array | null) - Optional - List of message content tokens containing log probability information. - `choices.logprobs.refusal` (array | null) - Optional - List of tokens containing refusal messages and their log probability information. - `choices.logprobs.refusal.token` (string) - Optional - Token. - `choices.logprobs.refusal.logprob` (number) - Optional - Log probability of the token (or -9999.0 if extremely unlikely). - `choices.logprobs.refusal.bytes` (array | null) - Optional - List of integers representing the UTF-8 byte representation of the token. - `choices.logprobs.refusal.top_logprobs` (array) - Optional - List of most likely tokens and their log probabilities at the current token position. - `created` (integer) - Required - The timestamp of when the response was generated (Unix timestamp in seconds). - `model` (string) - Required - The name of the model used. - `object` (enum) - Required - The type of the response. Possible value: `chat.completion.chunk`. - `tool_calls` (object[]) - Optional - Model-generated tool calls, such as function calls. - `tool_calls.function` (object) - Optional - The function called by the model. - `tool_calls.function.arguments` (string) - Optional - The arguments for the function call, generated by the model in JSON format. Validation is required. - `tool_calls.function.name` (string) - Optional - The name of the function to call. - `tool_calls.id` (string) - Optional - The unique identifier of the tool call. - `tool_calls.type` (enum) - Optional - The type of tool. Currently, only `function` is supported. - `usage` (object) - Required - Token usage statistics. - `usage.completion_tokens` (integer) - Required - The number of tokens used in the completion. - `usage.prompt_tokens` (integer) - Required - The number of tokens used in the prompt. - `usage.total_tokens` (integer) - Required - The total number of tokens used (prompt + completion). - `delta` (object) - Optional - The incremental chat completion generated by the streaming model response. - `finish_reason` (string | null) - Optional - The reason the model stopped generating tokens. - `index` (integer) - Optional - The index of the option in the options list. - `service_tier` (string | null) - Optional - The service tier used to handle the request. - `system_fingerprint` (string) - Optional - A fingerprint representing the backend configuration of the model runtime. ### Response Example ```json {"id":"","object":"chat.completion.chunk","created":1694268190,"model":"", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]} {"id":"","object":"chat.completion.chunk","created":1694268190,"model":"", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"content":"Hello"},"logprobs":null,"finish_reason":null}]} .... {"id":"","object":"chat.completion.chunk","created":1694268190,"model":"", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]} ``` ``` -------------------------------- ### Chat Completion Response Parameters (Non-Streaming) Source: https://platform.iflow.cn/docs/api-reference Describes the parameters returned in a non-streaming response from the Chat Completion API. Includes information about choices, usage, model, and tool calls. ```APIDOC ## Chat Completion Response (Non-Streaming) ### Description Details the structure of the JSON response when using a non-streaming request to the Chat Completion API. ### Parameters - `choices` (object[]) - Required - A list of choices generated by the model. - `choices.finish_reason` (enum) - Optional - The reason the generation ended. Possible values: `stop`, `eos`, `length`, `tool_calls`. - `choices.message` (object) - Required - The message object returned by the model. - `created` (integer) - Required - The timestamp of when the response was generated. - `id` (string) - Required - The unique identifier of the response. - `model` (string) - Required - The name of the model used. - `object` (enum) - Required - The type of the response. Possible value: `chat.completion`. - `tool_calls` (object[]) - Optional - Model-generated tool calls, such as function calls. - `tool_calls.function` (object) - Optional - The function called by the model. - `tool_calls.function.arguments` (string) - Optional - The arguments for the function call, generated by the model in JSON format. Validation is required. - `tool_calls.function.name` (string) - Optional - The name of the function to call. - `tool_calls.id` (string) - Optional - The unique identifier of the tool call. - `tool_calls.type` (enum) - Optional - The type of tool. Currently, only `function` is supported. - `usage` (object) - Required - Token usage statistics. - `usage.completion_tokens` (integer) - Required - The number of tokens used in the completion. - `usage.prompt_tokens` (integer) - Required - The number of tokens used in the prompt. - `usage.total_tokens` (integer) - Required - The total number of tokens used. ### Response Example ```json { "id": "", "choices": [ { "message": { "role": "assistant", "content": "", "reasoning_content": "" }, "finish_reason": "stop" } ], "tool_calls": [ { "id": "", "type": "function", "function": { "name": "", "arguments": "" } } ], "usage": { "prompt_tokens": 123, "completion_tokens": 123, "total_tokens": 123 }, "created": 123, "model": "", "object": "chat.completion" } ``` ``` -------------------------------- ### POST /v1/chat/completions Source: https://platform.iflow.cn/docs/api-reference Creates a text conversation request by sending messages to the API. Supports various parameters to control model behavior, response format, and more. ```APIDOC ## POST /v1/chat/completions ### Description This endpoint allows you to create a text-based conversation request. You can send a list of messages, specify the model to use, and configure various parameters to control the generation process, such as token limits, temperature, and response formatting. ### Method POST ### Endpoint `https://apis.iflow.cn/v1/chat/completions` ### Headers - **Authorization** (string) - Required - Use `Bearer ` for authentication. - **Content-Type** (string) - Required - Must be `application/json`. ### Request Body - **model** (enum) - Required - The name of the model to use (e.g., `tstars2.0`). - **messages** (object[]) - Required - A list of messages that constitute the conversation. - **content** (string) - Required - The content of the message. - **role** (enum) - Required - The role of the message author. Possible values: `user`, `assistant`, `system`. - **frequency_penalty** (number) - Optional - Penalizes new tokens based on their existing frequency in the text (default: `0.5`). - **max_tokens** (integer) - Optional - The maximum number of tokens to generate (range: `1 < x < 8192`). - **n** (integer) - Optional - The number of chat completion choices to generate for each input message (default: `1`). - **response_format** (object) - Optional - Specifies the format the model needs to output. - **type** (string) - Optional - The type of the response format. - **stop** (string[]) - Optional - The sequence where the API will stop generating further tokens. - **stream** (boolean) - Optional - Whether to stream back partial progress. If set, tokens will be sent as server-sent events (SSE) (default: `false`). - **temperature** (number) - Optional - Controls the randomness of the generated output. Lower values make the output more deterministic, while higher values make it more random (default: `0.7`). - **tools** (object[]) - Optional - A list of tools the model may call. Currently, only functions are supported. - **function** (object) - The definition of a function that the model may generate JSON inputs for. - **name** (string) - Required - The name of the function to be called. Must be a-z, A-Z, 0-9, or contain `_` and `-`, with a maximum length of 64. - **description** (string) - Optional - The description of the function, used by the model to decide when and how to call it. - **parameters** (object) - The parameters the function accepts, described as a JSON Schema object. - **strict** (boolean) - Optional - Whether to inject JSON into the tool's function call. If set to `true`, the tool will be injected with JSON. - **type** (enum) - Optional - The type of the tool. Currently only `function` is supported. - **top_k** (number) - Optional - Filters the set of tokens to choose from to the top k most likely tokens (default: `50`). - **top_p** (number) - Optional - An alternative to sampling with temperature, called nucleus sampling, where the model considers only the tokens comprising the top p probability mass (default: `0.7`). ### Request Example ```json { "model": "tstars2.0", "messages": [ { "role": "user", "content": "中国大模型行业2025年将会迎来哪些机遇和挑战?" } ], "max_tokens": 512, "temperature": 0.7 } ``` ### Response #### Success Response (200) - **id** (string) - Unique identifier for the response. - **object** (string) - Type of the object, typically `chat.completion`. - **created** (integer) - Timestamp of when the completion was created. - **model** (string) - The model used for the completion. - **choices** (array) - A list of completion choices. - **index** (integer) - The index of the choice in the list. - **message** (object) - The message content from the model. - **role** (string) - The role of the message author, usually `assistant`. - **content** (string) - The content of the message from the model. - **logprobs** (any) - Null or object, depending on the model and parameters used. - **finish_reason** (string) - The reason the model stopped generating tokens (e.g., `stop`, `length`). #### Response Example ```json { "id": "chatcmpl-12345", "object": "chat.completion", "created": 1677652288, "model": "tstars2.0", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "2025年,中国大模型行业将迎来机遇与挑战并存的局面。" }, "logprobs": null, "finish_reason": "stop" } ] } ``` ``` -------------------------------- ### POST /v1/chat/completions Source: https://platform.iflow.cn/docs/api-reference This endpoint allows you to send a chat completion request to the IFlow API. You can specify the model, messages, and various parameters to control the generation process. ```APIDOC ## POST /v1/chat/completions ### Description Sends a chat completion request to the IFlow API. ### Method POST ### Endpoint https://apis.iflow.cn/v1/chat/completions ### Parameters #### Request Body - **model** (string) - Required - The model to use for chat completions. - **messages** (array) - Required - A list of messages comprising the conversation. - **role** (string) - Required - The role of the author of the message. - **content** (string) - Required - The content of the message. - **stream** (boolean) - Optional - Whether to stream the response. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate. - **stop** (array) - Optional - Sequences where the API will stop generating further tokens. - **temperature** (number) - Optional - Controls randomness. Lower values make output more focused and deterministic. - **top_p** (number) - Optional - Controls nucleus sampling. An alternative to sampling with temperature, called nucleus sampling, where the model considers only the tokens comprising the top p probability mass. - **top_k** (integer) - Optional - Controls the number of top tokens to consider. - **frequency_penalty** (number) - Optional - Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. - **n** (integer) - Optional - How many chat completion choices to generate for each input message. - **response_format** (object) - Optional - Specifies the format of the response. - **type** (string) - Required - The type of response format (e.g., `text`). - **tools** (array) - Optional - A list of tools the model may call. - **type** (string) - Required - The type of tool (e.g., `function`). - **function** (object) - Required - The definition of the function that the model may generate JSON inputs for. - **description** (string) - Optional - Description of the function. - **name** (string) - Optional - The name of the function to be called. - **parameters** (object) - Optional - The parameters the function accepts. - **strict** (boolean) - Optional - Whether to enforce strict adherence to the parameters. ### Request Example ```json { "model": "tstars2.0", "messages": [ { "role": "user", "content": "中国大模型行业2025年将会迎来哪些机遇和挑战?" } ], "stream": false, "max_tokens": 512, "stop": [ "null" ], "temperature": 0.7, "top_p": 0.7, "top_k": 50, "frequency_penalty": 0.5, "n": 1, "response_format": { "type": "text" }, "tools": [ { "type": "function", "function": { "description": "", "name": "", "parameters": {}, "strict": false } } ] } ``` ### Response #### Success Response (200) (Response structure not provided in the input text) #### Response Example (Response example not provided in the input text) ``` -------------------------------- ### Streaming API Response Chunks (JSON) Source: https://platform.iflow.cn/docs/api-reference This shows the structure of individual chunks received in a streaming API response. Each chunk contains partial information about the chat completion, including deltas for role and content, log probabilities, and finish reasons. This format allows for real-time processing of model output. ```json {"id":"","object":"chat.completion.chunk","created":1694268190,"model":"", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]} {"id":"","object":"chat.completion.chunk","created":1694268190,"model":"", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"content":"Hello"},"logprobs":null,"finish_reason":null}]} .... {"id":"","object":"chat.completion.chunk","created":1694268190,"model":"", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]} ``` -------------------------------- ### Non-Streaming API Response Structure (JSON) Source: https://platform.iflow.cn/docs/api-reference This JSON object represents a typical non-streaming API response for chat completions. It includes fields like message content, tool calls, usage statistics, creation timestamp, model name, and response type. The structure is designed to provide a complete response in a single API call. ```json { "id": "", "choices": [ { "message": { "role": "assistant", "content": "", "reasoning_content": "" }, "finish_reason": "stop" } ], "tool_calls": [ { "id": "", "type": "function", "function": { "name": "", "arguments": "" } } ], "usage": { "prompt_tokens": 123, "completion_tokens": 123, "total_tokens": 123 }, "created": 123, "model": "", "object": "chat.completion" } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.