Converts text into a high-dimensional vector (embedding) that can be used for various natural language processing (NLP) tasks, such as calculating text similarity, clustering, and searching.
Table. AIOS Supported API List
Rerank API
POST/rerank,/v1/rerank,/v2/rerank
Overview
The Rerank API applies an embedding model or cross-encoder model to predict the relevance between a single query and each item in a document list.
Generally, the score of a sentence pair represents the similarity between the two sentences on a scale of 0 to 1.
Embedding-based model: Converts the query and document into vectors and measures the similarity between the vectors (e.g., cosine similarity) to calculate the score.
Reranker (Cross-Encoder) based model: Evaluates the query and document as a pair.
"query": "What is the capital of France?",
"documents": [
"The capital of France is Paris.",
"France capital city is known for the Eiffel Tower.",
"Paris is located in the north-central part of France."
],
"top_n": 2,
"truncate_prompt_tokens": 512
}
Response
200 OK
Name
Type
Description
id
string
API response’s unique identifier (UUID format)
model
string
Name of the model that generated the result
usage
integer
Object containing information about the resources used in the request
usage.total_tokens
integer
Total number of tokens used in processing the request
result
string
Array containing the results of the query-related documents
results[].index
integer
Order number in the result array
results[].document
object
Object containing the content of the searched document
results[].document.text
string
Actual text content of the searched document
results[].relevance_score
float
Score indicating the relevance between the query and the document (0 ~ 1)
Table. Re-rank API - 200 OK
Error Code
HTTP status code
Error Code Description
400
Bad Request
422
Validation Error
500
Internal Server Error
Table. Re-rank API - Error Code
Example
{"id":"rerank-scp-aios-rerank","model":"sds/sds/bge-m3","usage":{"total_tokens":65},"results":[{"index":0,"document":{"text":"The capital of France is Paris."},"relevance_score":0.8291233777999878},{"index":1,"document":{"text":"France capital city is known for the Eiffel Tower."},"relevance_score":0.6996355652809143}]}
curl-X'POST' \
'https://aios.private.kr-west1.e.samsungsdscloud.com/score' \
-H'accept: application/json' \
-H'Content-Type: application/json' \
-d'{"model":"sds/bge-reranker-v2-m3","encoding_format":"float","text_1":["What is the largest planet in the solar system?","What is the chemical symbol for water?"],"text_2":["Jupiter is the largest planet in the solar system.","The chemical symbol for water is H₂O."]}'
## Reference
* [Score API vLLM documentation](https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html#score-api_1)
# Chat Completions API
```python
POST /v1/chat/completions
Overview
Chat Completions API is compatible with OpenAI’s Completions API and can be used with the OpenAI Python client.
Request
Context
Key
Type
Description
Example
Content-Type
string
application/json
Table. Chat Completions API - Context
Path Parameters
Name
type
Required
Description
Default value
Boundary value
Example
None
Table. Chat Completions API - Path Parameters
Query Parameters
Name
type
Required
Description
Default value
Boundary value
Example
None
Table. Chat Completions API - Query Parameters
Body Parameters
Name
Name Sub
type
Required
Description
Default value
Boundary value
Example
model
-
string
✅
Specifies the model to use for generating responses
“meta-llama/Llama-3.3-70B-Instruct”
messages
role
string
✅
List of messages containing conversation history
[ { “role” : “user” , “content” : “message” }]
frequency_penalty
-
number
❌
Adjusts the penalty for repeating tokens
0
-2.0 ~ 2.0
0.5
logit_bias
-
object
❌
Adjusts the probability of specific tokens (e.g., { “100”: 2.0 })
null
Key: token ID, Value: -100 ~ 100
{ “100”: 2.0 }
logprobs
-
boolean
❌
Returns the probabilities of the top logprobs number of tokens
false
true, false
true
max_completion_tokens
-
integer
❌
Limits the maximum number of generated tokens
None
0 ~ model maximum
100
max_tokens (Deprecated)
-
integer
❌
Limits the maximum number of generated tokens
None
0 ~ model maximum
100
n
-
integer
❌
Specifies the number of responses to generate
1
3
presence_penalty
-
number
❌
Adjusts the penalty for tokens already present in the text
0
-2.0 ~ 2.0
1.0
seed
-
integer
❌
Specifies the seed value for controlling randomness
None
stop
-
string / array / null
❌
Stops generating when a specific string is encountered
null
"\n"
stream
-
boolean
❌
Returns the result in streaming mode
false
true/false
true
stream_options
include_usage, continuous_usage_stats
object
❌
Controls streaming options (e.g., including usage statistics)
null
{ “include_usage”: true }
temperature
-
number
❌
Adjusts the creativity of the generated response (higher means more random)
1
0.0 ~ 1.0
0.7
tool_choice
-
string
❌
Specifies which tool to call
none: Does not call any tool
auto: Model decides whether to call a tool or generate a message
required: Model calls at least one tool
No tool: none
With tool: auto
tools
-
array
❌
List of tools that the model can call
Only functions are supported as tools
Supports up to 128 functions
None
top_logprobs
-
integer
❌
Specifies the number of top logprobs tokens to return (between 0 and 20)
Each is associated with a log probability value
logprobs must be set to true
Shows the probability values for the top k completions
None
0 ~ 20
3
top_p
-
number
❌
Limits the sampling probability of tokens (higher means more tokens are considered)
Tool call information (may be included depending on the model/settings)
choices[].finish_reason
string or null
Reason why the response was terminated (e.g., “stop”, “length”, etc.)
choices[].stop_reason
object or null
Additional termination reason details
choices[].logprobs
object or null
Token-wise log probability information (may be included depending on the settings)
usage
object
Token usage statistics
usage.prompt_tokens
integer
Number of tokens used in the input prompt
usage.completion_tokens
integer
Number of tokens used in the generated response
usage.total_tokens
integer
Total number of tokens (input + output)
Table. Chat Completions API - 200 OK
Error Code
HTTP status code
Error Code Description
400
Bad Request
422
Validation Error
500
Internal Server Error
Table. Chat Completions API - Error Code
Example
{"id":"chatcmpl-scp-aios-chat-completions","object":"chat.completion","created":1749702816,"model":"meta-llama/Meta-Llama-3.3-70B-Instruct","choices":[{"index":0,"message":{"role":"assistant","reasoning_content":null,"content":"The capital of Korea is Seoul.","tool_calls":[]},"logprobs":null,"finish_reason":"stop","stop_reason":null}],"usage":{"prompt_tokens":54,"total_tokens":62,"completion_tokens":8,"prompt_tokens_details":null},"prompt_logprobs":null}
Adjust the probability of specific tokens (e.g., { “100”: 2.0 })
null
Key: token ID, Value: -100~100
{ “100”: 2.0 }
logprobs
-
integer
❌
Return the probabilities of the top logprobs tokens
null
1 ~ 5
5
max_completion_tokens
-
integer
❌
Limit the maximum number of generated tokens
None
0~model maximum value
100
max_tokens (Deprecated)
-
integer
❌
Limit the maximum number of generated tokens
None
0~model maximum value
100
n
-
integer
❌
Specify the number of responses to generate
1
3
presence_penalty
-
number
❌
Adjust the penalty for tokens already present in the text
0
-2.0 ~ 2.0
1.0
seed
-
integer
❌
Specify a seed value for randomness control
None
stop
-
string / array / null
❌
Stop generating when a specific string is encountered
null
"\n"
stream
-
boolean
❌
Whether to return the results in a streaming manner
false
true/false
true
stream_options
include_usage, continuous_usage_stats
object
❌
Control streaming options (e.g., include usage statistics)
null
{ “include_usage”: true }
temperature
-
number
❌
Control the creativity of the generated response (higher means more random)
1
0.0 ~ 1.0
0.7
top_p
-
number
❌
Limit the sampling probability of tokens (higher means more tokens considered)
1
0.0 ~ 1.0
0.9
Table. Completions API - Body Parameters
### Example
```python
curl -X 'POST' \
'https://aios.private.kr-west1.e.samsungsdscloud.com/v1/completions' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"model": "meta-llama/Meta-Llama-3.3-70B-Instruct",
"prompt": "What is the capital of Korea?",
"temperature": 0.7
}'
Response
200 OK
Name
Type
Description
id
string
Unique identifier of the response
object
string
Type of the response object (e.g., “text_completion”)
created
integer
Creation time (Unix timestamp, seconds)
model
string
Name of the model used
choices
array
List of generated response choices
choices[].index
number
Index of the choice
choices[].text
string
Generated text object
choices[].logprobs
object
Token-wise log probability information (included based on settings)
choices[].finish_reason
string or null
Reason why the response was terminated (e.g., “stop”, “length” etc.)
choices[].stop_reason
object or null
Additional termination reason details
choices[].prompt_logprobs
object or null
Log probability of input prompt tokens (may be null)
usage
object
Token usage statistics
usage.prompt_tokens
number
Number of tokens used in the input prompt
usage.total_tokens
number
Total number of tokens (input + output)
| usage.completion_tokens | number | Number of tokens used in the generated response |
| usage.prompt_tokens_details | object | Details of prompt token usage |
<div class="figure-caption">
Table. Completions API - 200 OK
</div>
Error Code
HTTP status code
Error Code Description
400
Bad Request
422
Validation Error
500
Internal Server Error
Table. Completions API - Error Code
Example
{"id":"cmpl-scp-aios-completions","object":"text_completion","created":1749702612,"model":"meta-llama/Meta-Llama-3.3-70B-Instruct","choices":[{"index":0,"text":" \nOur capital city is Seoul. \n\nA. 1\nB. ","logprobs":null,"finish_reason":"length","stop_reason":null,"prompt_logprobs":null}],"usage":{"prompt_tokens":9,"total_tokens":25,"completion_tokens":16,"prompt_tokens_details":null}}
The Embedding API converts text into high-dimensional vectors (embeddings) that can be used for various natural language processing (NLP) tasks, such as calculating text similarity, clustering, and search.
curl-X'POST' \
'https://aios.private.kr-west1.e.samsungsdscloud.com/v1/embedding' \
-H'accept: application/json' \
-H'Content-Type: application/json' \
-d'{"model":"sds/bge-m3","input":"What is the capital of France?","encoding_format":"float"}'
Response
200 OK
Name
Type
Description
id
string
Unique identifier of the response
object
string
Type of the response object (e.g., “list”)
created
number
Creation time (Unix timestamp, seconds)
model
string
Name of the model used
data
array
Array of objects containing embedding results
data.index
number
Index of the input text (e.g., order of input texts)
data.object
string
Type of data item
data.embedding
array
Embedding vector values of the input text (sds-bge-m3 is a 1024-dimensional float array)
AIOS models are compatible with OpenAI’s API, so they are also compatible with OpenAI’s SDK.
The following is a list of OpenAI and Cohere compatible APIs supported by Samsung Cloud Platform AIOS service.
Converts text into a high-dimensional vector (embedding) that can be used for various natural language processing (NLP) tasks such as text similarity calculation, clustering, and search.
Applies an embedding model or a cross-encoder model to predict the relevance between a single query and each item in a document list.
cohere
langchain-cohere
Table. Python SDK Compatible API List
Note
The SDK Reference guide is based on a Virtual Server environment with Python installed.
The actual execution may differ from the example in terms of token count and message content.
OpenAI SDK
Installing the openai Package
Install the OpenAI package.
pip install openai
Text Completion API
The Text Completion API generates a natural sentence that follows the given input string.
/v1/completions
Request
Note
The Text Completion API can only use strings as input values.
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for AIOS model calls.model="<<model>>"# Enter the model ID for AIOS model calls.client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")response=client.completions.create(model=model,prompt="Hi")
Reference
The aios endpoint-url and model ID for model calls can be found in the LLM Endpoint Usage Guide on the resource details page. Refer to Using LLM.
Response
The text field in choices contains the model’s response.
Completion(id='cmpl-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',choices=[CompletionChoice(finish_reason='length',index=0,logprobs=None,text=' future president of the United States, I hope you’re doing well. As a',stop_reason=None,prompt_logprobs=None)],created=1750000000,model='<<model>>',object='text_completion',
stream request
stream can be used to receive the completed answer one by one, rather than receiving the entire answer at once, as the model generates tokens.
Request
Set the stream parameter value to True.
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# AIOS model call endpoint-url to be input for AIOS model callmodel="<<model>>"# AIOS model call model ID to be input for AIOS model callclient=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")response=client.completions.create(model=model,prompt="Hi",stream=True)# Receive the response as the model generates tokens.forchunkinresponse:print(chunk)
Response
Each token generates an answer, and each token can be checked in the choices’s text field.
The conversation completion API takes a list of messages in order as input and responds with a message that is suitable for the current context as the next order.
/v1/chat/completions
Request
Text message only, you can call as follows:
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# AIOS model call for aios endpoint-url to enter.model="<<model>>"# AIOS model call for model ID to enter.client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")response=client.chat.completions.create(model=model,messages=[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"Hi"}])
Note
Model call for aios endpoint-url and model ID information is provided in the resource details page’s LLM Endpoint usage guide. Please refer to Using LLM.
Response
You can check the model’s answer in the choices’s message.
ChatCompletion(id='chatcmpl-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',choices=[Choice(finish_reason='stop',index=0,logprobs=None,message=ChatCompletionMessage(content='Hello. How can I assist you today?',refusal=None,role='assistant',annotations=None,audio=None,function_call=None,tool_calls=[],reasoning_content=None),stop_reason=None)],created=1750000000,model='<<model>>',object='chat.completion',service_tier=None,system_fingerprint=None,usage=CompletionUsage(completion_tokens=10,prompt_tokens=42,total_tokens=52,completion_tokens_details=None,prompt_tokens_details=None),prompt_logprobs=None)
Stream Request
Using stream, you can wait for the model to generate all answers and receive the response at once, or receive and process the response for each token generated by the model.
Request
Enter True as the value of the stream parameter.
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# AIOS model call for aios endpoint-url to enter.model="<<model>>"# AIOS model call for model ID to enter.client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")response=client.chat.completions.create(model="meta-llama/Llama-3.3-70B-Instruct",messages=[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"Hi"}],stream=True)# You can receive a response each time the model generates a token.forchunkinresponse:print(chunk)
Response
Each token generates a response, and each token can be checked in the choices field of the delta field.
Tool calling refers to the interface of external tools defined outside the model, allowing the model to generate answers that can perform suitable tools in the current context.
Using tool call, you can define metadata for functions that the model can execute and utilize them to generate answers.
Request
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# AIOS model call endpoint URLmodel="<<model>>"# AIOS model IDclient=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")# Function to get weather informationtools=[{"type":"function","function":{"name":"get_weather","description":"Get current temperature for provided coordinates in celsius.","parameters":{"type":"object","properties":{"latitude":{"type":"number"},"longitude":{"type":"number"}},"required":["latitude","longitude"],"additionalProperties":False},"strict":True}}]
messages = [{“role”: “user”, “content”: “What is the weather like in Paris today?”}]
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools # Inform the model of the metadata of the tools that can be used.
)
Response
choices’s message.tool_calls can be used to check how the model determines the execution method of the tool.
In the following example, you can see that the tool_calls’s function uses the get_weather function and checks what arguments should be inserted.
After adding the result value of the function as a tool message and generating the model’s response again, you can create an answer using the result value.
Request
Based on tool_calls’s function.arguments in the response data, you can actually call the function.
importjson# example function, always responds with 14 degrees.defget_weather(latitude,longitude):return"14℃"tool_call=response.choices[0].message.tool_calls[0]args=json.loads(tool_call.function.arguments)result=get_weather(args["latitude"],args["longitude"])# "14℃"
After adding the result value of the function as a tool message to the conversation context and calling the model again,
the model can create an appropriate answer using the result value of the function.
# Add the model's tool call message to messagesmessages.append(response.choices[0].message)# Add the result of the actual function call to messagesmessages.append({"role":"tool","tool_call_id":tool_call.id,"content":str(result)})response_2=client.chat.completions.create(model=model,messages=messages,# tools=tools
Response
ChatCompletion(id='chatcmpl-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',choices=[Choice(finish_reason='stop',index=0,logprobs=None,message=ChatCompletionMessage(content='The current weather in Paris is 14℃.',refusal=None,role='assistant',annotations=None,audio=None,function_call=None,tool_calls=[],reasoning_content=None),stop_reason=None)],created=1750000000,model='<<model>>',object='chat.completion',service_tier=None,system_fingerprint=None,usage=CompletionUsage(completion_tokens=11,prompt_tokens=74,total_tokens=85,completion_tokens_details=None,prompt_tokens_details=None),prompt_logprobs=None)
Reasoning
Request
Reasoning is supported in models that provide a reasoning value, which can be checked as follows:
Note
Models that support reasoning may take longer to generate answers because they produce many tokens for reasoning.
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for AIOS model calls.model="<<model>>"# Enter the model ID for AIOS model calls.client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")response=client.chat.completions.create(model=model,messages=[{"role":"user","content":"9.11 and 9.8, which is greater?"}],)
Response
The choices of the message field can be checked to see the content and also the reasoning_content, which provides the reasoning tokens.
ChatCompletion(id='chatcmpl-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',choices=[Choice(finish_reason='stop',index=0,logprobs=None,message=ChatCompletionMessage(content='''
To determine whether 9.11 or 9.8 is larger, we compare the decimal parts since both numbers have the same whole number part (9).
1. Convert both numbers to the same decimal places for easier comparison:
- 9.11 remains as is.
- 9.8 can be written as 9.80.
2. Compare the tenths place:
- The tenths place of 9.11 is 1.
- The tenths place of 9.80 is 8.
3. Since 8 (from 9.80) is greater than 1 (from 9.11), 9.80 (or 9.8) is larger.
4. Verification by subtraction:
- Subtracting 9.11 from 9.8 gives \(9.80 - 9.11 = 0.69\), which is positive, confirming 9.8 is larger.
Thus, the larger number is \(\boxed{9.8}\).
''',refusal=None,role='assistant',annotations=None,audio=None,function_call=None,tool_calls=[],reasoning_content="""Okay, so I need to figure out whether 9.11 is bigger than 9.8 or vice versa.
Hmm, let me think. Both numbers start with 9, so the whole number part is the same.
Here is the translation of the given text:
That means the difference must be in the decimal parts.
First, I remember that when comparing decimals, you look at the digits one by one after the decimal point.
The first digit after the decimal is the tenths place, then hundredths, and so on.
Since both numbers have 9 in the units place, I can focus on the decimal parts: 0.11 versus 0.08.
Wait, let me write them out to be clear. 9.11 is 9 and 11 hundredths, and 9.8 is 9 and 8 tenths.
Wait a second, maybe I should convert them to the same decimal places to make it easier.
So, 9.8 is the same as 9.80, right? Because adding a zero at the end of a decimal doesn't change its value.
That way, both numbers have two decimal places. Now comparing 9.11 and 9.80.
Looking at the tenths place first: 9.11 has 1 tenth, and 9.80 has 8 tenths.
Since 8 is more than 1, that means 9.80 is larger than 9.11. Wait, but hold on, the original numbers were 9.11 and 9.8. Did I make a mistake here?
Let me double-check. 9.8 is indeed equal to 9.80.
So when comparing tenths, 8 tenths (from 9.80) is definitely more than 1 tenth (from 9.11). So that would mean 9.8 is bigger.
But wait, maybe I confused the decimal places. Let me think again.
The first number after the decimal is tenths, so in 9.11, the first digit is 1 (tenths place) and the second is 1 (hundredths).
In 9.8, the first digit is 8 (tenths place), and there's no hundredths, which is equivalent to 0.
So comparing the tenths place first: 1 vs 8. Since 8 is greater than 1, 9.8 is larger.
But why did I initially think maybe 9.11 was bigger? Maybe because 11 is a two-digit number?
But no, in decimals, each position is a different place value.
The tenths place is more significant than the hundredths. So even though 11 hundredths is 0.11, that's still less than 0.8 (which is 8 tenths).
Another way to think about it: Convert both to fractions.
9.11 is 9 + 11/100, and 9.8 is 9 + 8/10.
Converting 8/10 to hundredths, that's 80/100.
So 9.8 is 9 + 80/100, whereas 9.11 is 9 + 11/100. Comparing the fractions: 80/100 is more than 11/100, so 9.8 is bigger.
Alternatively, maybe subtract them to see the difference. 9.8 minus 9.11 is 0.69, which is positive, so 9.8 is larger.
Wait, let me do the subtraction step by step.
9.8 minus 9.11:
First, write them aligned by decimal:
9.80
-9.11
-------
0.69
Yes, so the result is positive, so 9.8 is bigger.
Alternatively, if I had to do it without converting, maybe I can think in terms of money.
If these were dollars, 9.11 is $9.11 and 9.8 is $9.80.
Clearly, $9.80 is more than $9.11.
Hmm, so all these methods point to 9.8 being larger.
Maybe my initial confusion was because I saw 11 as a two, but
...omitted...
**Final Answer**
The number 9.8 is larger than 9.11. This is because when comparing the decimal parts, 0.8 (from 9.8) is greater than 0.11 (from 9.11).
Specifically, 9.8 can be written as 9.80, and comparing the tenths place (8 vs. 1) shows that 9.8 is larger.
The difference between them is 0.69, confirming that 9.8 is indeed the larger number.
**Final Answer**
\\boxed{9.8}"""
),
stop_reason=None
)
### image to text
**vision**을 지원하는 모델의 경우, 다음과 같이 이미지를 입력할 수 있습니다.

<div class="scp-textbox scp-textbox-type-error">
<div class="scp-textbox-title">Note</div>
<div class="scp-textbox-contents">
<p>For models that support <strong>vision</strong>, there are limitations on the size and number of input images.</p>
<p>Please refer to <a href="/en/userguide/ai_ml/aios/overview/#provided-models">Provided Models</a> for more information on image input limitations.</p>
</div>
</div>
#### Request
You can input an image with **MIME type** and **base64**.
```python
import base64
from openai import OpenAI
from urllib.parse import urljoin
aios_base_url = "<<aios endpoint-url>>" # AIOS endpoint-url for model calls
model = "<<model>>" # Model ID for AIOS model calls
client = OpenAI(base_url=urljoin(aios_base_url, "v1"), api_key="EMPTY_KEY")
image_path = "image/path.jpg"
def encode_image(image_path: str):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
The following is an analysis of the image to generate text.
ChatCompletion(id='chatcmpl-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',choices=[Choice(finish_reason='stop',index=0,logprobs=None,message=ChatCompletionMessage(content="""Here's what's in the image:
* **A golden retriever puppy:** The main subject is a light-colored golden retriever puppy lying on green grass.
* **A bone:** The puppy is holding a large bone in its paws and appears to be enjoying chewing on it.
* **Grass:** The puppy is lying on a well-maintained lawn.
* **Vegetation:** Behind the puppy, there are some shrubs and other greenery.
* **Outdoor setting:** The scene is outdoors, likely a backyard.""",refusal=None,role='assistant',annotations=None,audio=None,function_call=None,tool_calls=[],reasoning_content=None),stop_reason=106)],created=1750000000,model='<<model>>',object='chat.completion',service_tier=None,system_fingerprint=None,usage=CompletionUsage(completion_tokens=114,prompt_tokens=276,total_tokens=390,completion_tokens_details=None,prompt_tokens_details=None),prompt_logprobs=None,kv_transfer_params=None)
Embeddings API
Embeddings converts input text into a high-dimensional vector of a fixed dimension. The generated vector can be used for various natural language processing tasks such as text similarity, clustering, and search.
/v1/embeddings
Request
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# AIOS endpoint-url for model callsmodel="<<model>>"# Model ID for AIOS model callsclient=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")response=client.embeddings.create(input="What is the capital of France?",model=model)
Note
The aios endpoint-url and model ID for model calls can be found in the LLM Endpoint Usage Guide on the resource details page. Refer to Using LLM.
Response
data receives the converted value in vector form as a response.
The Cohere SDK can be used by installing the Cohere package.
pip install cohere
Rerank API
Rerank calculates the relevance between the given query and documents, and ranks them.
It can help improve the performance of RAG (Retrieval-Augmented Generation) structure applications by adjusting relevant documents to the front.
/v2/rerank
Request
importcoherefromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for AIOS model calls.model="<<model>>"# Enter the model ID for AIOS model calls.client=cohere.ClientV2("EMPTY_KEY",base_url=aios_base_url)docs=["The capital of France is Paris.","France capital city is known for the Eiffel Tower.","Paris is located in the north-central part of France."]response=client.rerank(model=model,query="What is the capital of France?",documents=docs,top_n=3,)
Note
The aios endpoint-url and model ID information for model calls are provided in the LLM Endpoint Usage Guide on the resource details page. Refer to Using LLM.
Response
In results, you can check the documents sorted in order of relevance to the query.
V2RerankResponse(id='rerank-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',results=[V2RerankResponseResultsItem(document=V2RerankResponseResultsItemDocument(text='The capital of France is Paris.'),index=0,relevance_score=1.0),V2RerankResponseResultsItem(
Here is the translated text:
document=V2RerankResponseResultsItemDocument(
text='France capital city is known for the Eiffel Tower.'
),
index=1,
relevance_score=1.0
),
V2RerankResponseResultsItem(
document=V2RerankResponseResultsItemDocument(
text='Paris is located in the north-central part of France.'
),
index=2,
relevance_score=0.982421875
)
The langchain-openai package can be used to utilize the text completion API and conversation completion API.
langchain_openai.OpenAI
When the text completion model (langchain_openai.OpenAI) is invoked, the result value is generated as text.
Request
fromlangchain_openaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for AIOS model calls.model="<<model>>"# Enter the model ID for AIOS model calls.llm=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY",model=model)llm.invoke("Can you introduce yourself in 5 words?")
Response
"""Hi, I'm a fun artist!
...omitted..."""
Note
The aios endpoint-url and model ID information for model calls are provided in the LLM Endpoint Usage Guide on the resource details page. Refer to Using LLM.
langchain_openai.ChatOpenAI
When the conversation completion model (langchain_openai.ChatOpenAI) is invoked, the result value is generated as an AIMessage or Message object.
Request
fromlangchain_openaiimportChatOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for AIOS model calls.model="<<model>>"# Enter the model ID for AIOS model calls.chat_llm=ChatOpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY",model=model)chat_completion=chat_llm.invoke("Can you introduce yourself in 5 words?")chat_completion.pretty_print()
Note
Information for the aios endpoint-url and model ID for model invocation can be found in the LLM Endpoint usage guide on the resource details page. Please refer to Using LLM.
Response
================================== Ai Message ==================================
I am an AI assistant.
embeddings
Embeddings models such as langchain-together, langchain-fireworks can be used.
Request
fromlangchain_togetherimportTogetherEmbeddingsfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for AIOS model invocation.model="<<model>>"# Enter the model ID for AIOS model invocation.embedding=TogetherEmbeddings(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY",model=model)embedding.embed_query("What is the capital of France?")
Note
Information for the aios endpoint-url and model ID for model invocation can be found in the LLM Endpoint usage guide on the resource details page. Please refer to Using LLM.
Rerank models can utilize langchain-cohere’s CohereRerank.
Request
fromlangchain_cohere.rerankimportCohereRerankaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for AIOS model invocation.model="<<model>>"# Enter the model ID for AIOS model invocation.rerank=CohereRerank(base_url=aios_base_url,cohere_api_key="EMPTY_KEY",model=model)docs=["The capital of France is Paris.","France capital city is known for the Eiffel Tower.","Paris is located in the north-central part of France."]rerank.rerank(documents=docs,query="What is the capital of France?",top_n=3)
Note
Information for the aios endpoint-url and model ID for model invocation can be found in the LLM Endpoint usage guide on the resource details page. Please refer to Using LLM.
This tutorial introduces how to create and utilize a web-based Playground to easily test the APIs of various AI models provided by AIOS using Streamlit in an SCP for Enterprise environment.
Environment
To proceed with this tutorial, the following environment must be prepared:
System Environment
Python 3.10 +
pip
Required installation packages
Color mode
pip install streamlit
pip install streamlit
Code Block. Install streamlit package
Note
Streamlit Python-based open-source web application framework, it is a very suitable tool for visually expressing and sharing data science, machine learning, and data analysis results. Without complex web development knowledge, you can quickly create a web interface by writing just a few lines of code.
Implementation
Pre-check
The application checks if the model call is normal with curl in the environment where it is running. Here, AIOS_LLM_Private_Endpoint refers to the LLM usage guide please refer to it.
Example: {AIOS LLM Private Endpoint}/{API}
Color mode
curl -H "Content-Type: application/json"\
-d '{"model": "meta-llama/Llama-3.3-70B-Instruct"
, "prompt" : "Hello, I am jihye, who are you"
, "temperature": 0
, "max_tokens": 100
, "stream": false}' -L AIOS_LLM_Private_Endpoint
curl -H "Content-Type: application/json"\
-d '{"model": "meta-llama/Llama-3.3-70B-Instruct"
, "prompt" : "Hello, I am jihye, who are you"
, "temperature": 0
, "max_tokens": 100
, "stream": false}' -L AIOS_LLM_Private_Endpoint
Code Block. CURL Model Call Example
choices’s text field contains the model’s answer, which can be confirmed.
{"id":"cmpl-4ac698a99c014d758300a3ec5583d73b","object":"text_completion","created":1750140201,"model":"meta-llama/Llama-3.3-70B-Instruct","choices":[{"index":0,"text":"?\nI am a student who is studying English.\nI am interested in learning about different cultures and making friends from around the world.\nI like to watch movies, listen to music, and read books in my free time.\nI am looking forward to chatting with you and learning more about your culture and way of life.\nNice to meet you, jihye! I'm happy to chat with you and learn more about culture. What kind of movies, music, and books do you enjoy? Do","logprobs":null,"finish_reason":"length","stop_reason":null,"prompt_logprobs":null}],"usage":{"prompt_tokens":11,"total_tokens":111,"completion_tokens":100}}
Project Structure
chat-playground
├── app.py # streamlit main web app file
├── endpoints.json # AIOS model's call type definition
├── img
│ └── aios.png
└── models.json # AIOS model list
Chat Playground code
Reference
models.json, endpoints.json files must exist and be configured in the appropriate format, please refer to the code below.
코드 내 BASE_URL 은 LLM 이용 가이드를 참고하여 AIOS LLM Private Endpoint 주소로 수정해야 합니다 should be translated to: - The BASE_URL in the code must be modified to the AIOS LLM Private Endpoint address, referring to the LLM usage guide.
This Playground is designed with a one-time request-based structure, so users can provide input values, press a button, send a request once, and check the result in this way, which allows for quick testing and response verification without complex session management.
The parameters of Model, Type, Temperature, Max Tokens configured in the sidebar are an interface configured through st.sidebar, and can be freely extended or modified as needed.
st.file_uploader() uploaded images (files) exist as temporary BytesIO objects on the server memory and are not automatically saved to disk.
app.py
streamlit main web app file. here, the BASE_URL, AIOS_LLM_Private_Endpoint, please refer to the LLM usage guide
Color mode
import streamlit as st
import base64
import json
import requests
from urllib.parse import urljoin
BASE_URL="AIOS_LLM_Private_Endpoint"# ===== Setting =====st.set_page_config(page_title="AIOS Chat Playground", layout="wide")st.title("🤖 AIOS Chat Playground")# ===== Common Functions =====def load_models():
with open("models.json", "r") as f:
return json.load(f)def load_endpoints():
with open("endpoints.json", "r") as f:
return json.load(f)models= load_models()endpoints_config= load_endpoints()# ===== Sidebar Settings =====st.sidebar.title('Hello!')st.sidebar.image("img/aios.png")st.sidebar.header("⚙️ Setting")model= st.sidebar.selectbox("Model", models)endpoint_labels=[ep["label"]for ep in endpoints_config]endpoint_label= st.sidebar.selectbox("Type", endpoint_labels)selected_endpoint= next(ep for ep in endpoints_config if ep["label"]== endpoint_label)temperature= st.sidebar.slider("🔥 Temperature", 0.0, 1.0, 0.7)max_tokens= st.sidebar.number_input("🧮 Max Tokens", min_value=1, max_value=5000, value=100)base_url= BASE_URL
path= selected_endpoint["path"]endpoint_type= selected_endpoint["type"]api_style= selected_endpoint.get("style", "openai")# openai or cohere# ===== Input UI =====prompt=""docs=[]image_base64= None
ifendpoint_type=="image":
prompt= st.text_area("✍️ Enter your question:", "Explain this image.")uploaded_image= st.file_uploader("🖼️ Upload an image", type=["png", "jpg", "jpeg"])if uploaded_image:
st.image(uploaded_image, caption="Uploaded image", use_container_width=300)image_bytes= uploaded_image.read()image_base64= base64.b64encode(image_bytes).decode("utf-8")elifendpoint_type=="rerank":
prompt= st.text_area("✍️ Enter your query:", "What is the capital of France?")raw_docs= st.text_area("📄 Documents (one per line)", "The capital of France is Paris.\nFrance capital city is known for the Eiffel Tower.\nParis is located in the north-central part of France.")docs= raw_docs.strip().splitlines()elifendpoint_type=="reasoning":
prompt= st.text_area("✍️ Enter prompt:", "9.11 and 9.8, which is greater?")elifendpoint_type=="embedding":
prompt= st.text_area("✍️ Enter prompt:", "What is the capital of France?")else:
prompt= st.text_area("✍️ Enter prompt:", "Hello, who are you?")uploaded_image= st.file_uploader("🖼️ Upload an image (Optional)", type=["png", "jpg", "jpeg"])if uploaded_image:
image_bytes= uploaded_image.read()image_base64= base64.b64encode(image_bytes).decode("utf-8")# ===== Call Button =====if st.button("🚀 Invoke model"):
headers={"Content-Type": "application/json",
"Authorization": "Bearer EMPTY_KEY"} try:
ifendpoint_type=="chat":
url= urljoin(base_url, "v1/chat/completions")payload={"model": model,
"messages": [{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}],
"temperature": temperature,
"max_tokens": max_tokens
}elifendpoint_type=="completion":
url= urljoin(base_url, "v1/completions")payload={"model": model,
"prompt": prompt,
"temperature": temperature,
"max_tokens": max_tokens
}elifendpoint_type=="embedding":
url= urljoin(base_url, "v1/embeddings")payload={"model": model,
"input": prompt
}elifendpoint_type=="reasoning":
url= urljoin(BASE_URL, "v1/chat/completions")payload={"model": model,
"messages": [{"role": "user", "content": prompt}],
"temperature": temperature,
"max_tokens": max_tokens
}elifendpoint_type=="image":
url= urljoin(base_url, "v1/chat/completions")if not image_base64:
st.warning("🖼️ Upload an image") st.stop()payload={"model": model,
"messages": [{"role": "user",
"content": [{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}}]}]}elifendpoint_type=="rerank":
url= urljoin(base_url, "v2/rerank")payload={"model": model,
"query": prompt,
"documents": docs,
"top_n": len(docs)}else:
st.error("❌ Unknown endpoint type") st.stop() st.expander("📤 Request payload").code(json.dumps(payload, indent=2), language="json")response= requests.post(url, headers=headers, json=payload) response.raise_for_status()res= response.json()# ===== Response Parsing =====ifendpoint_type=="chat" or endpoint_type=="image":
output= res["choices"][0]["message"]["content"]elifendpoint_type=="completion":
output= res["choices"][0]["text"]elifendpoint_type=="embedding":
vec= res["data"][0]["embedding"]output= f"🔢 Vector dimensions: {len(vec)}" st.expander("📐 Vector preview").code(vec[:20])elifendpoint_type=="rerank":
results= res["results"]output="\n\n".join([f"{i+1}. The document text (score: {r['relevance_score']:.3f})"for i, r in enumerate(results)])elifendpoint_type=="reasoning":
message= res.get("choices", [{}])[0].get("message", {})reasoning= message.get("reasoning_content", "❌ No reasoning_content")content= message.get("content", "❌ No content")output= f"""📘 <b>response:</b><br>{content}<br><br>🧠 <b>Reasoning:</b><br>{reasoning}""" st.success("✅ Model response:") st.markdown(f"<div style='padding:1rem;background:#f0f0f0;border-radius:8px'>{output}</div>", unsafe_allow_html=True) st.expander("📦 View full response").json(res) except requests.RequestException as e:
st.error("❌ Request failed") st.code(str(e))
import streamlit as st
import base64
import json
import requests
from urllib.parse import urljoin
BASE_URL="AIOS_LLM_Private_Endpoint"# ===== Setting =====st.set_page_config(page_title="AIOS Chat Playground", layout="wide")st.title("🤖 AIOS Chat Playground")# ===== Common Functions =====def load_models():
with open("models.json", "r") as f:
return json.load(f)def load_endpoints():
with open("endpoints.json", "r") as f:
return json.load(f)models= load_models()endpoints_config= load_endpoints()# ===== Sidebar Settings =====st.sidebar.title('Hello!')st.sidebar.image("img/aios.png")st.sidebar.header("⚙️ Setting")model= st.sidebar.selectbox("Model", models)endpoint_labels=[ep["label"]for ep in endpoints_config]endpoint_label= st.sidebar.selectbox("Type", endpoint_labels)selected_endpoint= next(ep for ep in endpoints_config if ep["label"]== endpoint_label)temperature= st.sidebar.slider("🔥 Temperature", 0.0, 1.0, 0.7)max_tokens= st.sidebar.number_input("🧮 Max Tokens", min_value=1, max_value=5000, value=100)base_url= BASE_URL
path= selected_endpoint["path"]endpoint_type= selected_endpoint["type"]api_style= selected_endpoint.get("style", "openai")# openai or cohere# ===== Input UI =====prompt=""docs=[]image_base64= None
ifendpoint_type=="image":
prompt= st.text_area("✍️ Enter your question:", "Explain this image.")uploaded_image= st.file_uploader("🖼️ Upload an image", type=["png", "jpg", "jpeg"])if uploaded_image:
st.image(uploaded_image, caption="Uploaded image", use_container_width=300)image_bytes= uploaded_image.read()image_base64= base64.b64encode(image_bytes).decode("utf-8")elifendpoint_type=="rerank":
prompt= st.text_area("✍️ Enter your query:", "What is the capital of France?")raw_docs= st.text_area("📄 Documents (one per line)", "The capital of France is Paris.\nFrance capital city is known for the Eiffel Tower.\nParis is located in the north-central part of France.")docs= raw_docs.strip().splitlines()elifendpoint_type=="reasoning":
prompt= st.text_area("✍️ Enter prompt:", "9.11 and 9.8, which is greater?")elifendpoint_type=="embedding":
prompt= st.text_area("✍️ Enter prompt:", "What is the capital of France?")else:
prompt= st.text_area("✍️ Enter prompt:", "Hello, who are you?")uploaded_image= st.file_uploader("🖼️ Upload an image (Optional)", type=["png", "jpg", "jpeg"])if uploaded_image:
image_bytes= uploaded_image.read()image_base64= base64.b64encode(image_bytes).decode("utf-8")# ===== Call Button =====if st.button("🚀 Invoke model"):
headers={"Content-Type": "application/json",
"Authorization": "Bearer EMPTY_KEY"} try:
ifendpoint_type=="chat":
url= urljoin(base_url, "v1/chat/completions")payload={"model": model,
"messages": [{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}],
"temperature": temperature,
"max_tokens": max_tokens
}elifendpoint_type=="completion":
url= urljoin(base_url, "v1/completions")payload={"model": model,
"prompt": prompt,
"temperature": temperature,
"max_tokens": max_tokens
}elifendpoint_type=="embedding":
url= urljoin(base_url, "v1/embeddings")payload={"model": model,
"input": prompt
}elifendpoint_type=="reasoning":
url= urljoin(BASE_URL, "v1/chat/completions")payload={"model": model,
"messages": [{"role": "user", "content": prompt}],
"temperature": temperature,
"max_tokens": max_tokens
}elifendpoint_type=="image":
url= urljoin(base_url, "v1/chat/completions")if not image_base64:
st.warning("🖼️ Upload an image") st.stop()payload={"model": model,
"messages": [{"role": "user",
"content": [{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}}]}]}elifendpoint_type=="rerank":
url= urljoin(base_url, "v2/rerank")payload={"model": model,
"query": prompt,
"documents": docs,
"top_n": len(docs)}else:
st.error("❌ Unknown endpoint type") st.stop() st.expander("📤 Request payload").code(json.dumps(payload, indent=2), language="json")response= requests.post(url, headers=headers, json=payload) response.raise_for_status()res= response.json()# ===== Response Parsing =====ifendpoint_type=="chat" or endpoint_type=="image":
output= res["choices"][0]["message"]["content"]elifendpoint_type=="completion":
output= res["choices"][0]["text"]elifendpoint_type=="embedding":
vec= res["data"][0]["embedding"]output= f"🔢 Vector dimensions: {len(vec)}" st.expander("📐 Vector preview").code(vec[:20])elifendpoint_type=="rerank":
results= res["results"]output="\n\n".join([f"{i+1}. The document text (score: {r['relevance_score']:.3f})"for i, r in enumerate(results)])elifendpoint_type=="reasoning":
message= res.get("choices", [{}])[0].get("message", {})reasoning= message.get("reasoning_content", "❌ No reasoning_content")content= message.get("content", "❌ No content")output= f"""📘 <b>response:</b><br>{content}<br><br>🧠 <b>Reasoning:</b><br>{reasoning}""" st.success("✅ Model response:") st.markdown(f"<div style='padding:1rem;background:#f0f0f0;border-radius:8px'>{output}</div>", unsafe_allow_html=True) st.expander("📦 View full response").json(res) except requests.RequestException as e:
st.error("❌ Request failed") st.code(str(e))
Code Block. app.py
models.json
AIOS model list. Refer to the LLM usage guide to set the model to be used.
Color mode
["meta-llama/Llama-3.3-70B-Instruct",
"qwen/Qwen3-30B-A3B",
"qwen/QwQ-32B",
"google/gemma-3-27b-it",
"meta-llama/Llama-4-Scout",
"meta-llama/Llama-Guard-4-12B",
"sds/bge-m3",
"sds/bge-reranker-v2-m3"There is no Korean text to translate.
["meta-llama/Llama-3.3-70B-Instruct",
"qwen/Qwen3-30B-A3B",
"qwen/QwQ-32B",
"google/gemma-3-27b-it",
"meta-llama/Llama-4-Scout",
"meta-llama/Llama-Guard-4-12B",
"sds/bge-m3",
"sds/bge-reranker-v2-m3"There is no Korean text to translate.
Code Block. models.json
endpoints.json
The call type of the AIOS model is defined, and the input screen and result are output differently according to the type.
Color mode
[{"label": "Chat Model",
"path": "/v1/chat/completions",
"type": "chat"},
{"label": "Completion Model",
"path": "/v1/completions",
"type": "completion"},
{"label": "Embedding Model",
"path": "/v1/embeddings",
"type": "embedding"},
{"label": "Image Chat Model",
"path": "/v1/chat/completions",
"type": "image"},
{"label": "Rerank Model",
"path": "/v2/rerank",
"type": "rerank"},
{"label": "Reasoning Model",
"path": "/v1/chat/completions",
"type": "reasoning"}There is no Korean text to translate.
[{"label": "Chat Model",
"path": "/v1/chat/completions",
"type": "chat"},
{"label": "Completion Model",
"path": "/v1/completions",
"type": "completion"},
{"label": "Embedding Model",
"path": "/v1/embeddings",
"type": "embedding"},
{"label": "Image Chat Model",
"path": "/v1/chat/completions",
"type": "image"},
{"label": "Rerank Model",
"path": "/v2/rerank",
"type": "rerank"},
{"label": "Reasoning Model",
"path": "/v1/chat/completions",
"type": "reasoning"}There is no Korean text to translate.
Code Block. endpoints.json
Playground usage method
This document covers two ways to run Playground.
Run on Virtual Server
1. Running Streamlit on a Virtual Server
Color mode
streamlit run app.py --server.port 8501 --server.address 0.0.0.0
streamlit run app.py --server.port 8501 --server.address 0.0.0.0
Code Block. Streamlit Execution
You can now view your Streamlit app in your browser.
URL: http://0.0.0.0:8501
Access from http://{your_server_ip}:8501 in the browser or after setting up server SSH tunneling http://localhost:8501. Refer to the following for SSH tunneling:
2. Accessing Virtual Server through tunneling on a local PC (when accessing http://localhost:8501)
1. Deployment and Service startup The following YAML is executed to start the Deployment and Service. It provides a container image packaged with code and Python library files to run the Chat Playground tutorial.
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
streamlit-deployment-8bfcd5959-6xpx9 1/1 Running 0 17s
$ kubectl logs streamlit-deployment-8bfcd5959-6xpx9
Collecting usage statistics. To deactivate, set browser.gatherUsageStats to false.
You can now view your Streamlit app in your browser.
URL: http://0.0.0.0:8501
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 172.20.0.1 <none> 443/TCP 46h
streamlit-service NodePort 172.20.95.192 <none> 80:30081/TCP 130m
You can access it from the browser at http://{worker_node_ip}:30081 or after setting up the server SSH tunneling at http://localhost:8501. Please refer to the following for SSH tunneling.
2. Accessing worker nodes through tunneling on a local PC (when accessing http://localhost:8501)
Code block. Tunneling to worker node through relay server from local PC
Usage example
Main screen composition
Item
Description
1
Model
This is a list of callable models set in the models.json file.
2
Endpoint type
must be selected according to the model call type set in the endpoints.json file to match the model
3
Temperature
The parameter that controls the degree of “randomness” or “creativity” of the model output. In this tutorial, it is specified in the range of 0.00 ~ 1.00.
0.0 : Only the most likely token is selected → Accurate and consistent response, lack of diversity
0.7 : Moderate randomness → Balance between creativity and consistency
1.0 : High randomness → Diverse and creative responses, possible quality variation
4
Max Tokens
Sets the maximum number of tokens that can be generated in the response text as an output length limit parameter. In this tutorial, it is specified in the range of 1 to 5000.
5
Input Area
The way to receive prompts, images, etc. varies depending on the endpoint type.
Chat, Completion, Embedding. Reasoning : general text input
Image : text + image upload
Rerank : query + document list (in this tutorial, line-by-line text is recognized as a document)
Fig. Main screen composition
Calling the Chat Model
Image model calling
Reasoning model calling
Conclusion
Through this tutorial, I hope you have learned how to build and utilize a Playground UI that can easily test various AI model APIs provided by AIOS, and you can flexibly customize it to fit your desired model and endpoint structure for actual service purposes.
Using the AI model provided by AIOS, create an Autogen AI Agent application.
Note
Autogen Autogen is an open-source framework that can easily build and manage LLM-based multi-agent collaboration and event-driven automation workflows.
environment
To proceed with this tutorial, the following environment must be prepared.
Code block. autogen, mcp server package installation
System Architecture
Shows the entire flow of the agent architecture using multi AI agent architecture and MCP.
Travel Planning Agent Flow
The user requests to set up a 3-day Nepal travel plan
Groupchat manger adjusts the execution order of registered agents (travel plan, local information, travel conversation, comprehensive summary)
Each agent performs the given tasks collaboratively according to its respective role
Once the final travel plan result is derived, deliver it to the user
MCP Flow
Note
MCP MCP(Model Context Protocol) is an open standard protocol that coordinates interactions between the model and external data or tools.
The MCP server is a server that implements this, using tool metadata to mediate and execute function calls.
User queries about the current time in Korea
mcp_server_time model request including metadata of a tool that can retrieve the current time via the server
get_current_time calling the function tool calls message generation
Through the MCP server, by executing the get_current_time function and passing the result to the model request, generate the final response and deliver it to the user.
Implementation
Travel Planning Agent
Reference
Please refer to the LLM usage guide for the AIOS_BASE_URL AIOS_LLM_Private_Endpoint and the MODEL_ID of the MODEL.
autogen_travel_planning.py
Color mode
fromurllib.parseimporturljoinfromautogen_agentchat.agentsimportAssistantAgentfromautogen_agentchat.conditionsimportTextMentionTerminationfromautogen_agentchat.teamsimportRoundRobinGroupChatfromautogen_agentchat.uiimportConsolefromautogen_ext.models.openaiimportOpenAIChatCompletionClientfromautogen_core.modelsimportModelFamily# Set the API URL and model name for model access.AIOS_BASE_URL="AIOS_LLM_Private_Endpoint"MODEL="MODEL_ID"# Create a model client using OpenAIChatCompletionClient.model_client=OpenAIChatCompletionClient(model=MODEL,base_url=urljoin(AIOS_BASE_URL,"v1"),api_key="EMPTY_KEY",model_info={# Set to True if images are supported."vision":False,# Set to True if function calls are supported."function_calling":True,# Set to True if JSON output is supported."json_output":True,# If the model you want to use is not provided by ModelFamily, use UNKNOWN.# "family": ModelFamily.UNKNOWN,"family":ModelFamily.LLAMA_3_3_70B,# Set to True if supporting structured output."structured_output":True,},)# Create multiple agents.# Each agent performs roles such as travel planning, local activity recommendations, providing language tips, and summarizing travel plans.planner_agent=AssistantAgent("planner_agent",model_client=model_client,description="A helpful assistant that can plan trips.",system_message=("You are a helpful assistant that can suggest a travel plan ""for a user based on their request."),)local_agent=AssistantAgent("local_agent",model_client=model_client,description="A local assistant that can suggest local activities or places to visit.",system_message=("You are a helpful assistant that can suggest authentic and """interesting local activities or places to visit for a user ""and can utilize any context information provided."),)language_agent=AssistantAgent("language_agent",model_client=model_client,description="A helpful assistant that can provide language tips for a given destination.",system_message=("You are a helpful assistant that can review travel plans, ""providing feedback on important/critical tips about how best to address """language or communication challenges for the given destination. """If the plan already includes language tips, ""you can mention that the plan is satisfactory, with rationale."),)travel_summary_agent=AssistantAgent("travel_summary_agent",model_client=model_client,description="A helpful assistant that can summarize the travel plan.",system_message=("You are a helpful assistant that can take in all of the suggestions ""and advice from the other agents and provide a detailed final travel plan. """You must ensure that the final plan is integrated and complete. ""YOUR FINAL RESPONSE MUST BE THE COMPLETE PLAN. ""When the plan is complete and all perspectives are integrated, ""you can respond with TERMINATE."),)# Group the agents and create a RoundRobinGroupChat.# RoundRobinGroupChat adjusts so that agents perform tasks in the order they are registered, taking turns.# This group enables agents to interact and make travel plans.# The termination condition uses TextMentionTermination to end the group chat when the text "TERMINATE" is mentioned.termination=TextMentionTermination("TERMINATE")group_chat=RoundRobinGroupChat([planner_agent,local_agent,language_agent,travel_summary_agent],termination_condition=termination,)asyncdefmain():"""Main function, runs group chat and makes travel plans."""# Run a group chat to make travel plans.# User requests the task "Plan a 3 day trip to Nepal."# Print the results using the console.awaitConsole(group_chat.run_stream(task="Plan a 3 day trip to Nepal."))awaitmodel_client.close()if__name__=="__main__":importasyncioasyncio.run(main())
fromurllib.parseimport urljoin
fromautogen_agentchat.agentsimport AssistantAgent
fromautogen_agentchat.conditionsimport TextMentionTermination
fromautogen_agentchat.teamsimport RoundRobinGroupChat
fromautogen_agentchat.uiimport Console
fromautogen_ext.models.openaiimport OpenAIChatCompletionClient
fromautogen_core.modelsimport ModelFamily
# Set the API URL and model name for model access.AIOS_BASE_URL ="AIOS_LLM_Private_Endpoint"MODEL ="MODEL_ID"# Create a model client using OpenAIChatCompletionClient.model_client = OpenAIChatCompletionClient(
model=MODEL,
base_url=urljoin(AIOS_BASE_URL, "v1"),
api_key="EMPTY_KEY",
model_info={
# Set to True if images are supported."vision": False,
# Set to True if function calls are supported."function_calling": True,
# Set to True if JSON output is supported."json_output": True,
# If the model you want to use is not provided by ModelFamily, use UNKNOWN.# "family": ModelFamily.UNKNOWN,"family": ModelFamily.LLAMA_3_3_70B,
# Set to True if supporting structured output."structured_output": True,
},
)
# Create multiple agents.# Each agent performs roles such as travel planning, local activity recommendations, providing language tips, and summarizing travel plans.planner_agent = AssistantAgent(
"planner_agent",
model_client=model_client,
description="A helpful assistant that can plan trips.",
system_message=("You are a helpful assistant that can suggest a travel plan ""for a user based on their request."),
)
local_agent = AssistantAgent(
"local_agent",
model_client=model_client,
description="A local assistant that can suggest local activities or places to visit.",
system_message=("You are a helpful assistant that can suggest authentic and """interesting local activities or places to visit for a user ""and can utilize any context information provided."),
)
language_agent = AssistantAgent(
"language_agent",
model_client=model_client,
description="A helpful assistant that can provide language tips for a given destination.",
system_message=("You are a helpful assistant that can review travel plans, ""providing feedback on important/critical tips about how best to address """language or communication challenges for the given destination. """If the plan already includes language tips, ""you can mention that the plan is satisfactory, with rationale."),
)
travel_summary_agent = AssistantAgent(
"travel_summary_agent",
model_client=model_client,
description="A helpful assistant that can summarize the travel plan.",
system_message=("You are a helpful assistant that can take in all of the suggestions ""and advice from the other agents and provide a detailed final travel plan. """You must ensure that the final plan is integrated and complete. ""YOUR FINAL RESPONSE MUST BE THE COMPLETE PLAN. ""When the plan is complete and all perspectives are integrated, ""you can respond with TERMINATE."),
)
# Group the agents and create a RoundRobinGroupChat.# RoundRobinGroupChat adjusts so that agents perform tasks in the order they are registered, taking turns.# This group enables agents to interact and make travel plans.# The termination condition uses TextMentionTermination to end the group chat when the text "TERMINATE" is mentioned.termination = TextMentionTermination("TERMINATE")
group_chat = RoundRobinGroupChat(
[planner_agent, local_agent, language_agent, travel_summary_agent],
termination_condition=termination,
)
asyncdefmain():
"""Main function, runs group chat and makes travel plans."""# Run a group chat to make travel plans.# User requests the task "Plan a 3 day trip to Nepal."# Print the results using the console.await Console(group_chat.run_stream(task="Plan a 3 day trip to Nepal."))
await model_client.close()
if __name__ =="__main__":
importasyncio asyncio.run(main())
Code block. autogen_travel_planning.py
When you run a file using python, you can see multiple agents working together, each performing its role for a single task.
Color mode
python autogen_travel_planning.py
python autogen_travel_planning.py
Code block. autogen travel plan agent execution
Execution Result
----------TextMessage(user)----------Plana3daytriptoNepal.----------TextMessage(planner_agent)----------Nepal!Acountrywitharichculturalheritage,breathtakingnaturalbeauty,andwarmhospitality.Here's a suggested 3-day itinerary for your trip to Nepal:**Day1:ArrivalinKathmanduandExplorationoftheCity***ArriveatTribhuvanInternationalAirportinKathmandu,thecapitalcityofNepal.*Check-intoyourhotelandfreshenup.*Visitthefamous**BoudhanathStupa**,oneofthelargestBuddhiststupasintheworld.*Explorethe**Thamel**area,apopulartouristhubknownforitsnarrowstreets,shops,andrestaurants.*Intheevening,enjoyatraditionalNepalidinnerandwatchaculturalperformanceatalocalrestaurant.**Day2:KathmanduValleyTour***Startthedaywithavisittothe**PashupatinathTemple**,asacredHindutemplededicatedtoLordShiva.*Next,headtothe**KathmanduDurbarSquare**,aUNESCOWorldHeritageSiteandtheformerroyalpalaceoftheMallakings.*Visitthe**SwayambhunathStupa**,alsoknownastheMonkeyTemple,whichoffersstunningviewsofthecity.*Intheafternoon,takeashortdrivetothe**PatanCity**,knownforitsrichculturalheritageandtraditionalcrafts.*Explorethe**PatanDurbarSquare**andvisitthe**KrishnaTemple**,abeautifulexampleofNepaliarchitecture.**Day3:BhaktapurandNagarkot***Driveto**Bhaktapur**,amedievaltownandaUNESCOWorldHeritageSite(approximately1hour).*Explorethe**BhaktapurDurbarSquare**,whichfeaturesstunningarchitecture,temples,andpalaces.*Visitthe**PotterySquare**,whereyoucanseetraditionalpottery-makingtechniques.*Intheafternoon,driveto**Nagarkot**,ascenichillstationwithbreathtakingviewsoftheHimalayas(approximately1.5hours).*WatchthesunsetovertheHimalayasandenjoythepeacefulatmosphere.**AdditionalTips:***MakesuretotrysomelocalNepalicuisine,suchasmomos,dalbhat,andgorkhalilamb.*Bargainwhileshoppinginthemarkets,asit's a common practice in Nepal.*Respectlocalcustomsandtraditions,especiallywhenvisitingtemplesandculturalsites.*Stayhydratedandbringsunscreen,asthesuncanbestronginNepal.**Accommodation:**Kathmanduhasawiderangeofaccommodationoptions,frombudget-friendlyguesthousestoluxuryhotels.SomepopularareastostayincludeThamel,Lazimpat,andBoudha.**Transportation:**Youcanhireataxioraprivatevehicleforthedaytotravelbetweendestinations.Alternatively,youcanusepublictransportation,suchasbusesormicrobuses,whichareaffordableandconvenient.**Budget:**Thebudgetfora3-daytriptoNepalcanvarydependingonyouraccommodationchoices,transportation,andactivities.However,here's a rough estimate:*Accommodation:$20-50pernight*Transportation:$10-20perday*Food:$10-20permeal*Activities:$10-20perpersonTotalestimatedbudgetfor3days:$200-500perpersonIhopethishelps,andyouhaveawonderfultriptoNepal!----------TextMessage(local_agent)----------Your3-dayitineraryforNepaliswell-plannedandcoversmanyofthecountry's cultural and natural highlights. Here are a few additional suggestions and tips to enhance your trip:**Day1:***AftervisitingtheBoudhanathStupa,considerexploringthesurroundingstreets,whicharefilledwithTibetanshops,restaurants,andmonasteries.*IntheThamelarea,besuretotrysomeofthelocalstreetfood,suchasmomosorselroti.*Fordinner,considertryingatraditionalNepalirestaurant,suchastheKathmanduGuestHouseortheNorthfieldCafe.**Day2:***AtthePashupatinathTemple,berespectfuloftheHinduritualsandcustoms.YoucanalsotakeastrollalongtheBagmatiRiver,whichrunsthroughthetemplecomplex.*AttheKathmanduDurbarSquare,considerhiringaguidetoprovidemoreinsightintothehistoryandsignificanceofthetemplesandpalaces.*Intheafternoon,visitthePatanMuseum,whichshowcasestheartandcultureoftheKathmanduValley.**Day3:***InBhaktapur,besuretotrysomeofthelocalpotteryandhandicrafts.YoucanalsovisittheBhaktapurNationalArtGallery,whichfeaturestraditionalNepaliart.*AtNagarkot,considertakingashorthiketothenearbyvillages,whichofferstunningviewsoftheHimalayas.*Forsunset,findaspotwithaclearviewofthemountains,andenjoythepeacefulatmosphere.**AdditionalTips:***Nepalisarelativelyconservativecountry,sodressmodestlyandrespectlocalcustoms.*TrytolearnsomebasicNepaliphrases,suchas"namaste"(hello)and"dhanyabaad"(thankyou).*Bepreparedforcrowdsandchaosinthecities,especiallyinThamelandKathmanduDurbarSquare.*ConsiderpurchasingalocalSIMcardorportableWi-Fihotspottostayconnectedduringyourtrip.**Accommodation:***Considerstayinginahotelorguesthousethatiscentrallylocatedandhasgoodreviews.*LookforaccommodationsthatofferamenitiessuchasfreeWi-Fi,hotwater,andarestaurantorcafe.**Transportation:***Considerhiringaprivatevehicleortaxifortheday,asthiswillgiveyoumoreflexibilityandconvenience.*Besuretonegotiatethepriceandagreeontheitinerarybeforesettingoff.**Budget:***Bepreparedforvariablepricesandexchangerates,andhavesomelocalcurrency(Nepalirupees)onhand.*Considerbudgetingextraforunexpectedexpenses,suchastransportationorfood.Overall,youritineraryprovidesagoodbalanceofculture,history,andnaturalbeauty,andwiththeseadditionaltipsandsuggestions,you'll be well-prepared for an unforgettable trip to Nepal!----------TextMessage(language_agent)----------Your3-dayitineraryforNepaliswell-plannedandcoversmanyofthecountry's cultural and natural highlights. The additional suggestions and tips you provided are excellent and will help enhance the trip experience.Oneaspectthatiswell-coveredinyourplanistheculturalandhistoricalsignificanceofthedestinations.Youhaveincludedamixoftemples,stupas,andculturalsites,whichwillgivevisitorsagoodunderstandingofNepal's rich heritage.Regardinglanguageandcommunicationchallenges,yourtipto"try to learn some basic Nepali phrases, such as 'namaste' (hello) and 'dhanyabaad' (thank you)"isexcellent.Thiswillhelpvisitorsshowrespectforthelocalcultureandpeople,andcanalsofacilitateinteractionswithlocals.Additionally,yoursuggestionto"consider purchasing a local SIM card or portable Wi-Fi hotspot to stay connected during your trip"ispracticalandwillhelpvisitorsstayintouchwithfamilyandfriendsbackhome,aswellasnavigatethelocalarea.Yourplanissatisfactory,andwiththeadditionaltipsandsuggestions,visitorswillbewell-preparedforanunforgettabletriptoNepal.Theitineraryprovidesagoodbalanceofculture,history,andnaturalbeauty,andthetipsonlanguage,communication,andlogisticswillhelpensureasmoothandenjoyablejourney.Overall,yourplaniswell-thought-out,andtheadditionalsuggestionsandtipswillhelpvisitorsmakethemostoftheirtriptoNepal.Welldone!However,oneminorsuggestionImightmakeistoconsiderincludingafewphrasesinthelocallanguageforemergencysituations,suchas"where is the hospital?"or"how do I get to the airport?"Thiscanhelpvisitorsincaseofanunexpectedsituation,andcanalsogivethemmoreconfidencewhennavigatingunfamiliarareas.Butoverall,yourplanisexcellent,andwiththeseminorsuggestions,itcanbeevenmorecomprehensiveandhelpfulforvisitorstoNepal.----------TextMessage(travel_summary_agent)----------TERMINATEHereisthecompleteandintegrated3-daytravelplantoNepal:**Day1:ArrivalinKathmanduandExplorationoftheCity***ArriveatTribhuvanInternationalAirportinKathmandu,thecapitalcityofNepal.*Check-intoyourhotelandfreshenup.*Visitthefamous**BoudhanathStupa**,oneofthelargestBuddhiststupasintheworld.*Explorethesurroundingstreets,whicharefilledwithTibetanshops,restaurants,andmonasteries.*Explorethe**Thamel**area,apopulartouristhubknownforitsnarrowstreets,shops,andrestaurants.Besuretotrysomeofthelocalstreetfood,suchasmomosorselroti.*Intheevening,enjoyatraditionalNepalidinnerandwatchaculturalperformanceatalocalrestaurant,suchastheKathmanduGuestHouseortheNorthfieldCafe.**Day2:KathmanduValleyTour***Startthedaywithavisittothe**PashupatinathTemple**,asacredHindutemplededicatedtoLordShiva.BerespectfuloftheHinduritualsandcustoms,andtakeastrollalongtheBagmatiRiver,whichrunsthroughthetemplecomplex.*Next,headtothe**KathmanduDurbarSquare**,aUNESCOWorldHeritageSiteandtheformerroyalpalaceoftheMallakings.Considerhiringaguidetoprovidemoreinsightintothehistoryandsignificanceofthetemplesandpalaces.*Visitthe**SwayambhunathStupa**,alsoknownastheMonkeyTemple,whichoffersstunningviewsofthecity.*Intheafternoon,visitthe**PatanCity**,knownforitsrichculturalheritageandtraditionalcrafts.Explorethe**PatanDurbarSquare**andvisitthe**KrishnaTemple**,abeautifulexampleofNepaliarchitecture.Also,visitthePatanMuseum,whichshowcasestheartandcultureoftheKathmanduValley.**Day3:BhaktapurandNagarkot***Driveto**Bhaktapur**,amedievaltownandaUNESCOWorldHeritageSite(approximately1hour).Explorethe**BhaktapurDurbarSquare**,whichfeaturesstunningarchitecture,temples,andpalaces.Besuretotrysomeofthelocalpotteryandhandicrafts,andvisittheBhaktapurNationalArtGallery,whichfeaturestraditionalNepaliart.*Intheafternoon,driveto**Nagarkot**,ascenichillstationwithbreathtakingviewsoftheHimalayas(approximately1.5hours).Considertakingashorthiketothenearbyvillages,whichofferstunningviewsoftheHimalayas.Findaspotwithaclearviewofthemountains,andenjoythepeacefulatmosphereduringsunset.**AdditionalTips:***MakesuretotrysomelocalNepalicuisine,suchasmomos,dalbhat,andgorkhalilamb.*Bargainwhileshoppinginthemarkets,asit's a common practice in Nepal.*Respectlocalcustomsandtraditions,especiallywhenvisitingtemplesandculturalsites.*Stayhydratedandbringsunscreen,asthesuncanbestronginNepal.*Dressmodestlyandrespectlocalcustoms,asNepalisarelativelyconservativecountry.*TrytolearnsomebasicNepaliphrases,suchas"namaste"(hello),"dhanyabaad"(thankyou),"where is the hospital?"and"how do I get to the airport?".*ConsiderpurchasingalocalSIMcardorportableWi-Fihotspottostayconnectedduringyourtrip.*Bepreparedforcrowdsandchaosinthecities,especiallyinThamelandKathmanduDurbarSquare.**Accommodation:***Considerstayinginahotelorguesthousethatiscentrallylocatedandhasgoodreviews.*LookforaccommodationsthatofferamenitiessuchasfreeWi-Fi,hotwater,andarestaurantorcafe.**Transportation:***Considerhiringaprivatevehicleortaxifortheday,asthiswillgiveyoumoreflexibilityandconvenience.*Besuretonegotiatethepriceandagreeontheitinerarybeforesettingoff.**Budget:***Thebudgetfora3-daytriptoNepalcanvarydependingonyouraccommodationchoices,transportation,andactivities.However,here's a rough estimate:+Accommodation:$20-50pernight+Transportation:$10-20perday+Food:$10-20permeal+Activities:$10-20perperson*Totalestimatedbudgetfor3days:$200-500perperson*Bepreparedforvariablepricesandexchangerates,andhavesomelocalcurrency(Nepalirupees)onhand.*Considerbudgetingextraforunexpectedexpenses,suchastransportationorfood.
Agent-specific conversation summary
Agent
Conversation summary
planner_agent
I propose a 3-day travel itinerary for Nepal.
Day 1: Arrival in Kathmandu and city exploration
Day 2: Kathmandu valley tour
Day 3: Visit Pokhara and Nagarkot
Additional tips: Respect local customs, try local food, choose transportation options, etc
local_agent
Based on planner_agent’s 3-day travel itinerary, we provide additional suggestions and tips.
Day 1: Explore around Budhanath Stupa,
Day 2: Respect Hindu rituals at Pashupatinath Temple
Day 3: Try pottery and handicrafts of Bhaktapur
Additional tips: Respect local customs, learn basic Nepali, use local facilities, etc
language_agent
Travel itinerary evaluation and provide additional suggestions. Basic Nepali learning, use of local facilities, language preparation for emergency situations, etc.
travel_summary_agent
Summarizes the overall 3-day travel plan.
Day 1: Arrival in Kathmandu and city exploration
Day 2: Kathmandu valley tour
Day 3: Visit Pokhara and Nagarkot
Additional tips: Respect local customs, try local food, choose transportation options, etc.
MCP Utilization Agent
Note
Please refer to the LLM usage guide for the AIOS_BASE_URL AIOS_LLM_Private_Endpoint and the MODEL_ID of the MODEL.
autogen_mcp.py
Color mode
fromurllib.parseimporturljoinfromautogen_core.modelsimportModelFamilyfromautogen_ext.models.openaiimportOpenAIChatCompletionClientfromautogen_ext.tools.mcpimportMcpWorkbench,StdioServerParamsfromautogen_agentchat.agentsimportAssistantAgentfromautogen_agentchat.uiimportConsole# Set the API URL and model name for model access.AIOS_BASE_URL="AIOS_LLM_Private_Endpoint"MODEL="MODEL_ID"# Create a model client using OpenAIChatCompletionClient.model_client=OpenAIChatCompletionClient(model=MODEL,base_url=urljoin(AIOS_BASE_URL,"v1"),api_key="EMPTY_KEY",model_info={# Set to True if images are supported."vision":False,# Set to True if function calls are supported."function_calling":True,# Set to True if JSON output is supported."json_output":True,# If the model you want to use is not provided by ModelFamily, use UNKNOWN.# "family": ModelFamily.UNKNOWN,"family":ModelFamily.LLAMA_3_3_70B,# Set to True if supporting structured output."structured_output":True,}")"# Set MCP server parameters.# mcp_server_time is an MCP server implemented in python,# It includes the get_current_time function that provides the current time internally, and the convert_time function that converts time zones.# This parameter sets the MCP server to the local timezone so that the time can be checked.# For example, if you set it to "Asia/Seoul", you can check the time according to the Korean time zone.mcp_server_params=StdioServerParams(command="python",args=["-m","mcp_server_time","--local-timezone","Asia/Seoul"],)asyncdefmain():"""Runs the agent that checks the time using the MCP workbench as the main function."""# Create and run an agent that checks the time using the MCP workbench.# The agent performs the task "What time is it now in South Korea?"# Print the results using the console.# while the MCP Workbench is running, the agent checks the time# Output the results in streaming mode.# If MCP Workbench terminates, the agent also terminates.asyncwithMcpWorkbench(mcp_server_params)asworkbench:time_agent=AssistantAgent("time_assistant",model_client=model_client,workbench=workbench,reflect_on_tool_use=True,)awaitConsole(time_agent.run_stream(task="What time is it now in South Korea?"))awaitmodel_client.close()if__name__=="__main__":importasyncioasyncio.run(main())
fromurllib.parseimport urljoin
fromautogen_core.modelsimport ModelFamily
fromautogen_ext.models.openaiimport OpenAIChatCompletionClient
fromautogen_ext.tools.mcpimport McpWorkbench, StdioServerParams
fromautogen_agentchat.agentsimport AssistantAgent
fromautogen_agentchat.uiimport Console
# Set the API URL and model name for model access.AIOS_BASE_URL ="AIOS_LLM_Private_Endpoint"MODEL ="MODEL_ID"# Create a model client using OpenAIChatCompletionClient.model_client = OpenAIChatCompletionClient(
model=MODEL,
base_url=urljoin(AIOS_BASE_URL, "v1"),
api_key="EMPTY_KEY",
model_info={
# Set to True if images are supported."vision": False,
# Set to True if function calls are supported."function_calling": True,
# Set to True if JSON output is supported."json_output": True,
# If the model you want to use is not provided by ModelFamily, use UNKNOWN.# "family": ModelFamily.UNKNOWN,"family": ModelFamily.LLAMA_3_3_70B,
# Set to True if supporting structured output."structured_output": True,
}
")"# Set MCP server parameters.# mcp_server_time is an MCP server implemented in python,# It includes the get_current_time function that provides the current time internally, and the convert_time function that converts time zones.# This parameter sets the MCP server to the local timezone so that the time can be checked.# For example, if you set it to "Asia/Seoul", you can check the time according to the Korean time zone.mcp_server_params = StdioServerParams(
command="python",
args=["-m", "mcp_server_time", "--local-timezone", "Asia/Seoul"],
)
asyncdefmain():
"""Runs the agent that checks the time using the MCP workbench as the main function."""# Create and run an agent that checks the time using the MCP workbench.# The agent performs the task "What time is it now in South Korea?"# Print the results using the console.# while the MCP Workbench is running, the agent checks the time# Output the results in streaming mode.# If MCP Workbench terminates, the agent also terminates.asyncwith McpWorkbench(mcp_server_params) as workbench:
time_agent = AssistantAgent(
"time_assistant",
model_client=model_client,
workbench=workbench,
reflect_on_tool_use=True,
)
await Console(time_agent.run_stream(task="What time is it now in South Korea?"))
await model_client.close()
if __name__ =="__main__":
importasyncio asyncio.run(main())
Code block. autogen_mcp.py
When you run the file using python, it fetches the tool’s metadata from the MCP server, calls the model, and when the model generates a tool calls message
You can see that the get_current_time function is executed to retrieve the current time.
# TextMessage (user): Input message given by the user----------TextMessage(user)----------WhattimeisitnowinSouthKorea?# Query metadata of tools that can be used on the MCP serverINFO:mcp.server.lowlevel.server:ProcessingrequestoftypeListToolsRequest...omission...INFO:autogen_core.events:{# Metadata of tools available on the MCP server"tools":[{"type":"function","function":{"name":"get_current_time","description":"Get current time in a specific timezones","parameters":{"type":"object","properties":{"timezone":{"type":"string","description":"IANA timezone name (e.g., 'America/New_York', 'Europe/London'). Use 'Asia/Seoul' as local timezone if no timezone provided by the user."}},"required":[""timezone],"additionalProperties":false},"strict":false}},{"type":"function","function":{"name":"convert_time","description":"Convert time between timezones","parameters":{"type":"object","properties":{"source_timezone":{"type":"string","description":"Source IANA timezone name (e.g., 'America/New_York', 'Europe/London'). Use 'Asia/Seoul' as local timezone if no source timezone provided by the user."},"time":{"type":"string","description":"Time to convert in 24-hour format (HH:MM)"},"target_timezone":{"type":"string","description":"Target IANA timezone name (e.g., 'Asia/Tokyo', 'America/San_Francisco'). Use 'Asia/Seoul' as local timezone if no target timezone provided by the user."}},"required":["source_timezone","time","target_timezone"],"additionalProperties":false},"strict":false}}],"type":"LLMCall",# input message"messages":[{"content":"You are a helpful AI assistant. Solve tasks using your tools. Reply with TERMINATE when the task has been completed.","role":"system"},{"role":"user","name":"user","content":"What time is it now in South Korea?"}],# Model Response"response":{"id":"chatcmpl-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","choices":[{"finish_reason":"tool_calls","index":0,"logprobs":null,"message":{"content":null,"refusal":null,"role":"assistant","annotations":null,"audio":null,"function_call":null,"tool_calls":["{"id":"chatcmpl-tool-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","function":{"arguments":"{\"timezone\": \"Asia/Seoul\"}","name":"get_current_time"},"type":"function"}],"reasoning_content":null},"stop_reason":128008}],"created":1751278737,"model":"MODEL_ID","object":"chat.completion","service_tier":null,"system_fingerprint":null,"usage":{"completion_tokens":21,"prompt_tokens":508,"total_tokens":529,"completion_tokens_details":null,"prompt_tokens_details":null},"prompt_logprobs":null},"prompt_tokens":508,"completion_tokens":21,"agent_id":null}# ToolCallRequestEvent: Receiving a tool call message from the model----------ToolCallRequestEvent(time_assistant)----------[FunctionCall(id='chatcmpl-tool-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',arguments='{"timezone": "Asia/Seoul"}',name='get_current_time')]INFO:mcp.server.lowlevel.server:ProcessingrequestoftypeListToolsRequest# Execute function of tool call message via MCP serverINFO:mcp.server.lowlevel.server:ProcessingrequestoftypeCallToolRequest# ToolCallExecutionEvent: Deliver the function execution result to the model----------ToolCallExecutionEvent(time_assistant)----------[FunctionExecutionResult(content='{"timezone":"Asia/Seoul","datetime":"2025-06-30T19:18:58+09:00","is_dst":false}', name='get_current_time', call_id='chatcmpl-tool-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', is_error=False)]...omission...# TextMessage (time_assistant): Final answer generated by the model----------TextMessage(time_assistant)----------ThecurrenttimeinSouthKoreais19:18:58KST.TERMINATE
MCP Server Time Query System Log Analysis Result
MCP(Model Control Protocol) server-based time query system execution process log analysis result.
Request Information
Item
Content
User request
What time is it now in South Korea?
Request Time
2025-06-30 19:18:58 KST
Processing method
MCP server tool call
Available tools
Tool Name
Description
Parameter
Default Value
get_current_time
Retrieve current time of a specific timezone
timezone (IANA timezone name)
Asia/Seoul
convert_time
Time conversion between time zones
source_timezone, time, target_timezone
Asia/Seoul
Processing steps
Step
Action
Details
1
Tool metadata lookup
Verify the list of tools available on the MCP server
2
AI model response
get_current_time function called in the Asia/Seoul timezone
3
Function execution
MCP server runs time lookup tool
4
Return result
Provide time information in structured JSON format
5
Final Answer
Deliver time to the user in an easy-to-read format
Function Call Details
Item
Value
function name
get_current_time
Parameter
{"timezone": "Asia/Seoul"}
Call ID
chatcmpl-tool-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
type
function
Execution result
Field
Value
Description
timezone
Asia/Seoul
Time zone
datetime
2025-06-30T19:18:58+09:00
ISO 8601 format time
is_dst
false
Daylight saving time applied
final response
Item
Content
Response Message
The current time in South Korea is 19:18:58 KST.
Completion mark
TERMINATE
Response Time
19:18:58 KST
Usage metric table
indicator
value
Prompt Token
508
completion token
21
Total token usage
529
Processing time
Immediate (real-time)
Main features
Feature
Description
MCP protocol utilization
Smooth integration with external tools
Korean time zone default setting
Asia/Seoul used as default
Structured response
Clear data return in JSON format
Auto-complete display
Work completion notification with TERMINATE
Real-time information provision
Accurate current time lookup
Technical significance
This is an example of a modern architecture where an AI assistant integrates with external systems to provide real-time information. Through MCP, the AI model can access various external tools and services, enabling more practical and dynamic responses.
Conclusion
In this tutorial, we implemented an application that creates travel itineraries using multiple agents by leveraging the AI model provided by AIOS and autogen, and an agent application that can use external tools by utilizing the MCP server. Through this, we learned that problems can be solved from multiple angles using several agents with different perspectives, and external tools can be utilized. This system can be expanded and customized to fit user environments in the following ways.
Agent flow control: Various techniques can be used when selecting the agent to perform the task. For reliable results, you can fix the order of agents and implement it, or you can let the AI model choose the agents for flexible processing. Additionally, you can use event techniques to implement multiple agents processing tasks in parallel.
Introduction of various MCP servers: In addition to mcp_server_time, various MCP servers that have already been implemented exist. By utilizing these, the AI model can flexibly use various external tools to implement useful applications.
Based on this tutorial, we hope you will directly build a suitable AIOS-based collaborative assistant according to the actual service purpose.
AIOS models are compatible with OpenAI and Cohere APIs, so they are also compatible with OpenAI and Cohere SDKs. The following is the list of OpenAI and Cohere compatible APIs supported by Samsung Cloud Platform AIOS service.
Converts text to high-dimensional vectors (embeddings), which can be used for various natural language processing (NLP) tasks such as text similarity calculation, clustering, and search.
go get github.com/openai/openai-go \
github.com/cohere-ai/cohere-go/v2
go get github.com/openai/openai-go \
github.com/cohere-ai/cohere-go/v2
Code Block. SDK package installation
Text Completion API
The Text Completion API generates natural sentences that immediately follow the given string as input.
non-stream request
Request
Caution
Text Completion API input can only use strings.
Color mode
importjsonimportrequestsfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# This includes the model ID to use and the prompt.data={"model":model,"prompt":"Hi"}# Send a POST request to the AIOS API's v1/completions endpoint.# Use urljoin function to combine the base URL and endpoint path.response=requests.post(urljoin(aios_base_url,"v1/completions"),json=data)# Parse the response body in JSON format.body=json.loads(response.text)# response.choices[0].text is the response text generated by the AI model.print(body["choices"][0]["text"])
importjsonimportrequestsfromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# This includes the model ID to use and the prompt.data = {
"model": model,
"prompt": "Hi"}
# Send a POST request to the AIOS API's v1/completions endpoint.# Use urljoin function to combine the base URL and endpoint path.response = requests.post(urljoin(aios_base_url, "v1/completions"), json=data)
# Parse the response body in JSON format.body = json.loads(response.text)
# response.choices[0].text is the response text generated by the AI model.print(body["choices"][0]["text"])
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")# Generate a completion using the AIOS model.# model parameter specifies the model ID to use,# prompt parameter is the input text to provide to the AI.response=client.completions.create(model=model,prompt="Hi")# response.choices[0].text is the response text generated by the AI model.print(response.choices[0].text)
fromopenaiimport OpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".client = OpenAI(base_url=urljoin(aios_base_url, "v1"), api_key="EMPTY_KEY")
# Generate a completion using the AIOS model.# model parameter specifies the model ID to use,# prompt parameter is the input text to provide to the AI.response = client.completions.create(
model=model,
prompt="Hi")
# response.choices[0].text is the response text generated by the AI model.print(response.choices[0].text)
fromlangchain_openaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create an LLM (Large Language Model) instance using LangChain's OpenAI class.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".# model parameter specifies the model ID to use.llm=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY",model=model)# Pass the prompt "Hi" to the LLM and receive a response.# The invoke method returns the model's output.print(llm.invoke("Hi"))
fromlangchain_openaiimport OpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create an LLM (Large Language Model) instance using LangChain's OpenAI class.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".# model parameter specifies the model ID to use.llm = OpenAI(
base_url=urljoin(aios_base_url, "v1"),
api_key="EMPTY_KEY",
model=model
)
# Pass the prompt "Hi" to the LLM and receive a response.# The invoke method returns the model's output.print(llm.invoke("Hi"))
constaios_base_url="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>"// Enter the model ID for calling the AIOS model.
// Configure the request data.
// This includes the model ID to use and the prompt.
constdata={model:model,prompt:"Hi",};// Create the AIOS API's v1/completions endpoint URL.
leturl=newURL("/v1/completions",aios_base_url);// Send a POST request to the AIOS API.
constresponse=awaitfetch(url,{method:"POST",headers:{"Content-Type":"application/json",},body:JSON.stringify(data),});// Parse the response body in JSON format.
constbody=awaitresponse.json();// response.choices[0].text is the response text generated by the AI model.
console.log(body.choices[0].text);
const aios_base_url ="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"// Enter the model ID for calling the AIOS model.
// Configure the request data.
// This includes the model ID to use and the prompt.
const data = {
model: model,
prompt:"Hi",
};
// Create the AIOS API's v1/completions endpoint URL.
let url =new URL("/v1/completions", aios_base_url);
// Send a POST request to the AIOS API.
const response =await fetch(url, {
method:"POST",
headers: {
"Content-Type":"application/json",
},
body: JSON.stringify(data),
});
// Parse the response body in JSON format.
const body =await response.json();
// response.choices[0].text is the response text generated by the AI model.
console.log(body.choices[0].text);
importOpenAIfrom"openai";constaios_base_url="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>"// Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
constclient=newOpenAI({apiKey:"EMPTY_KEY",baseURL:newURL("v1",aios_base_url).href,});// Generate a completion using the AIOS model.
// model parameter specifies the model ID to use,
// prompt parameter is the input text to provide to the AI.
constcompletions=awaitclient.completions.create({model:model,prompt:"Hi",});// response.choices[0].text is the response text generated by the AI model.
console.log(completions.choices[0].text);
import OpenAI from "openai";
const aios_base_url ="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"// Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
const client =new OpenAI({
apiKey:"EMPTY_KEY",
baseURL:new URL("v1", aios_base_url).href,
});
// Generate a completion using the AIOS model.
// model parameter specifies the model ID to use,
// prompt parameter is the input text to provide to the AI.
const completions =await client.completions.create({
model: model,
prompt:"Hi",
});
// response.choices[0].text is the response text generated by the AI model.
console.log(completions.choices[0].text);
import{OpenAI}from"@langchain/openai";constaios_base_url="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>"// Enter the model ID for calling the AIOS model.
// Create an LLM (Large Language Model) instance using LangChain's OpenAI class.
// model parameter specifies the model ID to use.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// configuration.baseURL points to the v1 endpoint of the AIOS API.
constllm=newOpenAI({model:model,apiKey:"EMPTY_KEY",configuration:{baseURL:newURL("v1",aios_base_url).href,},});// Pass the prompt "Hi" to the LLM and receive a streaming response.
// The stream method returns a stream that generates tokens in real-time.
constcompletion=awaitllm.invoke("Hi");// Output the generated response.
// This text is the response generated by the AI model.
console.log(completion);
import { OpenAI } from "@langchain/openai";
const aios_base_url ="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"// Enter the model ID for calling the AIOS model.
// Create an LLM (Large Language Model) instance using LangChain's OpenAI class.
// model parameter specifies the model ID to use.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// configuration.baseURL points to the v1 endpoint of the AIOS API.
const llm =new OpenAI({
model: model,
apiKey:"EMPTY_KEY",
configuration: {
baseURL:new URL("v1", aios_base_url).href,
},
});
// Pass the prompt "Hi" to the LLM and receive a streaming response.
// The stream method returns a stream that generates tokens in real-time.
const completion =await llm.invoke("Hi");
// Output the generated response.
// This text is the response generated by the AI model.
console.log(completion);
packagemainimport("bytes""encoding/json""fmt""io""net/http")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)// Define the data structure to be used for POST requests.
// Model: Model ID to use
// Prompt: Input text to provide to the AI
// Stream: Whether to stream response (optional)
typePostDatastruct{Modelstring`json:"model"`Promptstring`json:"prompt"`Streambool`json:"stream,omitempty"`}funcmain(){// Create request data.
data:=PostData{Model:model,Prompt:"Hi",}// Marshal data to JSON format.
jsonData,err:=json.Marshal(data)iferr!=nil{panic(err)}// Send a POST request to the AIOS API's v1/completions endpoint.
response,err:=http.Post(aiosBaseUrl+"/v1/completions","application/json",bytes.NewBuffer(jsonData))iferr!=nil{panic(err)}deferresponse.Body.Close()// Read the entire response body.
body,err:=io.ReadAll(response.Body)iferr!=nil{panic(err)}varvmap[string]interface{}json.Unmarshal(body,&v)// Extract the choices array from the response.
choices:=v["choices"].([]interface{})// Extract the first data's text.
choice:=choices[0].(map[string]interface{})text:=choice["text"]// Output the response text generated by the AI model.
fmt.Println(text)}
package main
import (
"bytes""encoding/json""fmt""io""net/http")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
// Define the data structure to be used for POST requests.
// Model: Model ID to use
// Prompt: Input text to provide to the AI
// Stream: Whether to stream response (optional)
type PostData struct {
Model string`json:"model"` Prompt string`json:"prompt"` Stream bool`json:"stream,omitempty"`}
funcmain() {
// Create request data.
data := PostData{
Model: model,
Prompt: "Hi",
}
// Marshal data to JSON format.
jsonData, err := json.Marshal(data)
if err !=nil {
panic(err)
}
// Send a POST request to the AIOS API's v1/completions endpoint.
response, err := http.Post(aiosBaseUrl +"/v1/completions", "application/json", bytes.NewBuffer(jsonData))
if err !=nil {
panic(err)
}
defer response.Body.Close()
// Read the entire response body.
body, err := io.ReadAll(response.Body)
if err !=nil {
panic(err)
}
var v map[string]interface{}
json.Unmarshal(body, &v)
// Extract the choices array from the response.
choices := v["choices"].([]interface{})
// Extract the first data's text.
choice := choices[0].(map[string]interface{})
text := choice["text"]
// Output the response text generated by the AI model.
fmt.Println(text)
}
packagemainimport("context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option""github.com/openai/openai-go/packages/param")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)funcmain(){// Create an OpenAI client.
// Use option.WithBaseURL to set the v1 endpoint of the AIOS API.
client:=openai.NewClient(option.WithBaseURL(aiosBaseUrl+"/v1"),)// Generate a completion using the AIOS model.
// Use openai.CompletionNewParams to set the model and prompt.
completion,err:=client.Completions.New(context.TODO(),openai.CompletionNewParams{Model:openai.CompletionNewParamsModel(model),Prompt:openai.CompletionNewParamsPromptUnion{OfString:param.Opt[string]{Value:"Hi"}},})iferr!=nil{panic(err)}// response.choices[0].text is the response text generated by the AI model.
fmt.Println(completion.Choices[0].Text)}
package main
import (
"context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option""github.com/openai/openai-go/packages/param")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
funcmain() {
// Create an OpenAI client.
// Use option.WithBaseURL to set the v1 endpoint of the AIOS API.
client := openai.NewClient(
option.WithBaseURL(aiosBaseUrl+"/v1"),
)
// Generate a completion using the AIOS model.
// Use openai.CompletionNewParams to set the model and prompt.
completion, err := client.Completions.New(context.TODO(), openai.CompletionNewParams{
Model: openai.CompletionNewParamsModel(model),
Prompt: openai.CompletionNewParamsPromptUnion{OfString: param.Opt[string]{Value: "Hi"}},
})
if err !=nil {
panic(err)
}
// response.choices[0].text is the response text generated by the AI model.
fmt.Println(completion.Choices[0].Text)
}
Code Block. /v1/completions request
Note
For information on the aios endpoint-url and model ID for calling the model, see the LLM Endpoint Usage Guide on the resource details page. Please refer to Using LLM.
Response
You can see that the model’s answer is included in the text field of choices.
future president of the United States, I hope you're doing well. As a
stream request
Using the stream feature, you can receive responses token by token as the model generates tokens, without waiting for the model to complete the entire response.
Request
Enter True for the stream parameter value.
Color mode
importjsonimportrequestsfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# This includes the model ID to use, the prompt, and whether to stream.data={"model":model,"prompt":"Hi","stream":True}# Send a POST request to the AIOS API's v1/completions endpoint.# Set stream=True to receive real-time streaming responses.response=requests.post(urljoin(aios_base_url,"v1/completions"),json=data,stream=True)# You can receive responses as the model generates tokens.# Responses are sent separated by each line, so process with iter_lines().forlineinresponse.iter_lines():ifline:try:# Remove the 'data: ' prefix and parse the JSON data.body=json.loads(line[len("data: "):])# response.choices[0].text is the response text generated by the AI model.print(body["choices"][0]["text"])except:pass
importjsonimportrequestsfromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# This includes the model ID to use, the prompt, and whether to stream.data = {
"model": model,
"prompt": "Hi",
"stream": True}
# Send a POST request to the AIOS API's v1/completions endpoint.# Set stream=True to receive real-time streaming responses.response = requests.post(urljoin(aios_base_url, "v1/completions"), json=data, stream=True)
# You can receive responses as the model generates tokens.# Responses are sent separated by each line, so process with iter_lines().for line in response.iter_lines():
if line:
try:
# Remove the 'data: ' prefix and parse the JSON data. body = json.loads(line[len("data: "):])
# response.choices[0].text is the response text generated by the AI model. print(body["choices"][0]["text"])
except:
pass
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")# Generate a completion using the AIOS model.# model parameter specifies the model ID to use,# prompt parameter is the input text to provide to the AI.# Set stream=True to receive real-time streaming responses.response=client.completions.create(model=model,prompt="Hi",stream=True)# You can receive responses as the model generates tokens.# response is sent in stream format, so you can process it iteratively.forchunkinresponse:# Each chunk's choices[0].text is the response text generated by the AI model.print(chunk.choices[0].text)
fromopenaiimport OpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".client = OpenAI(base_url=urljoin(aios_base_url, "v1"), api_key="EMPTY_KEY")
# Generate a completion using the AIOS model.# model parameter specifies the model ID to use,# prompt parameter is the input text to provide to the AI.# Set stream=True to receive real-time streaming responses.response = client.completions.create(
model=model,
prompt="Hi",
stream=True)
# You can receive responses as the model generates tokens.# response is sent in stream format, so you can process it iteratively.for chunk in response:
# Each chunk's choices[0].text is the response text generated by the AI model. print(chunk.choices[0].text)
fromlangchain_openaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create an LLM (Large Language Model) instance using LangChain's OpenAI class.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".# model parameter specifies the model ID to use.llm=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY",model=model)# Pass the prompt "Hi" to the LLM and receive a streaming response.# The stream method returns a stream that generates tokens in real-time.response=llm.stream("Hi")# You can receive responses as the model generates tokens.# response is sent in stream format, so you can process it iteratively.forchunkinresponse:# Output each chunk.# This chunk is the response token generated by the AI model.print(chunk)
fromlangchain_openaiimport OpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create an LLM (Large Language Model) instance using LangChain's OpenAI class.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".# model parameter specifies the model ID to use.llm = OpenAI(
base_url=urljoin(aios_base_url, "v1"),
api_key="EMPTY_KEY",
model=model
)
# Pass the prompt "Hi" to the LLM and receive a streaming response.# The stream method returns a stream that generates tokens in real-time.response = llm.stream("Hi")
# You can receive responses as the model generates tokens.# response is sent in stream format, so you can process it iteratively.for chunk in response:
# Output each chunk.# This chunk is the response token generated by the AI model. print(chunk)
constaios_base_url="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>"// Enter the model ID for calling the AIOS model.
// Configure the request data.
// This includes the model ID to use, the prompt, and whether to stream.
constdata={model:model,prompt:"Hi",stream:true,};// Create the AIOS API's v1/completions endpoint URL.
leturl=newURL("/v1/completions",aios_base_url);// Send a POST request to the AIOS API.
// Set stream: true to receive real-time streaming responses.
constresponse=awaitfetch(url,{method:"POST",headers:{"Content-Type":"application/json",},body:JSON.stringify(data),});// You can receive responses as the model generates tokens.
// Convert the response body to a text decoder stream and read it.
constreader=response.body.pipeThrough(newTextDecoderStream()).getReader();letbuf="";while(true){const{value,done}=awaitreader.read();if(done)break;// Add received data to buffer.
buf+=value;letsep;// Find newline characters (\n\n) in the buffer and separate data.
while((sep=buf.indexOf("\n\n"))>=0){constdata=buf.slice(0,sep);buf=buf.slice(sep+2);// Process each line.
for(constrawLineofdata.split("\n")){constline=rawLine.trim();if(!line.startsWith("data: "))continue;// Remove the "data: " prefix and extract JSON data.
constpayload=line.slice("data: ".length).trim();if(payload==="[DONE]")break;// Parse the JSON data.
constjson=JSON.parse(payload);// choices[0].text is the response text generated by the AI model.
console.log(json.choices[0].text);}}}
const aios_base_url ="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"// Enter the model ID for calling the AIOS model.
// Configure the request data.
// This includes the model ID to use, the prompt, and whether to stream.
const data = {
model: model,
prompt:"Hi",
stream:true,
};
// Create the AIOS API's v1/completions endpoint URL.
let url =new URL("/v1/completions", aios_base_url);
// Send a POST request to the AIOS API.
// Set stream: true to receive real-time streaming responses.
const response =await fetch(url, {
method:"POST",
headers: {
"Content-Type":"application/json",
},
body: JSON.stringify(data),
});
// You can receive responses as the model generates tokens.
// Convert the response body to a text decoder stream and read it.
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
let buf ="";
while (true) {
const { value, done } =await reader.read();
if (done) break;
// Add received data to buffer.
buf += value;
let sep;
// Find newline characters (\n\n) in the buffer and separate data.
while ((sep = buf.indexOf("\n\n")) >=0) {
const data = buf.slice(0, sep);
buf = buf.slice(sep +2);
// Process each line.
for (const rawLine of data.split("\n")) {
const line = rawLine.trim();
if (!line.startsWith("data: ")) continue;
// Remove the "data: " prefix and extract JSON data.
const payload = line.slice("data: ".length).trim();
if (payload ==="[DONE]") break;
// Parse the JSON data.
const json = JSON.parse(payload);
// choices[0].text is the response text generated by the AI model.
console.log(json.choices[0].text);
}
}
}
importOpenAIfrom"openai";constaios_base_url="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>"// Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
constclient=newOpenAI({apiKey:"EMPTY_KEY",baseURL:newURL("v1",aios_base_url).href,});// Generate a completion using the AIOS model.
// model parameter specifies the model ID to use,
// prompt parameter is the input text to provide to the AI.
// Set stream: true to receive real-time streaming responses.
constcompletions=awaitclient.completions.create({model:model,prompt:"Hi",stream:true,});// You can receive responses as the model generates tokens.
// Use for await...of loop to sequentially process stream events.
forawait(consteventofcompletions){// Each event's choices[0].text is the response text generated by the AI model.
console.log(event.choices[0].text);}
import OpenAI from "openai";
const aios_base_url ="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"// Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
const client =new OpenAI({
apiKey:"EMPTY_KEY",
baseURL:new URL("v1", aios_base_url).href,
});
// Generate a completion using the AIOS model.
// model parameter specifies the model ID to use,
// prompt parameter is the input text to provide to the AI.
// Set stream: true to receive real-time streaming responses.
const completions =await client.completions.create({
model: model,
prompt:"Hi",
stream:true,
});
// You can receive responses as the model generates tokens.
// Use for await...of loop to sequentially process stream events.
forawait (const event of completions) {
// Each event's choices[0].text is the response text generated by the AI model.
console.log(event.choices[0].text);
}
import{OpenAI}from"@langchain/openai";constaios_base_url="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>"// Enter the model ID for calling the AIOS model.
// Create an LLM (Large Language Model) instance using LangChain's OpenAI class.
// model parameter specifies the model ID to use.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// configuration.baseURL points to the v1 endpoint of the AIOS API.
constllm=newOpenAI({model:model,apiKey:"EMPTY_KEY",configuration:{baseURL:newURL("v1",aios_base_url).href,},});// Pass the prompt "Hi" to the LLM and receive a streaming response.
// The stream method returns a stream that generates tokens in real-time.
constcompletion=awaitllm.stream("Hi");// You can receive responses as the model generates tokens.
// Use for await...of loop to sequentially process stream chunks.
forawait(constchunkofcompletion){// Output each chunk.
// This chunk is the response token generated by the AI model.
console.log(chunk);}
import { OpenAI } from "@langchain/openai";
const aios_base_url ="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"// Enter the model ID for calling the AIOS model.
// Create an LLM (Large Language Model) instance using LangChain's OpenAI class.
// model parameter specifies the model ID to use.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// configuration.baseURL points to the v1 endpoint of the AIOS API.
const llm =new OpenAI({
model: model,
apiKey:"EMPTY_KEY",
configuration: {
baseURL:new URL("v1", aios_base_url).href,
},
});
// Pass the prompt "Hi" to the LLM and receive a streaming response.
// The stream method returns a stream that generates tokens in real-time.
const completion =await llm.stream("Hi");
// You can receive responses as the model generates tokens.
// Use for await...of loop to sequentially process stream chunks.
forawait (const chunk of completion) {
// Output each chunk.
// This chunk is the response token generated by the AI model.
console.log(chunk);
}
packagemainimport("bufio""bytes""encoding/json""fmt""net/http")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)// Define the data structure to be used for POST requests.
// Model: Model ID to use
// Prompt: Input text to provide to the AI
// Stream: Whether to stream response (optional)
typePostDatastruct{Modelstring`json:"model"`Promptstring`json:"prompt"`Streambool`json:"stream,omitempty"`}funcmain(){// Create request data.
// Set Stream: true to receive real-time streaming responses.
data:=PostData{Model:model,Prompt:"Hi",Stream:true,}// Marshal data to JSON format.
jsonData,err:=json.Marshal(data)iferr!=nil{panic(err)}// Send a POST request to the AIOS API's v1/completions endpoint.
response,err:=http.Post(aiosBaseUrl+"/v1/completions","application/json",bytes.NewBuffer(jsonData))iferr!=nil{panic(err)}deferresponse.Body.Close()// You can receive responses as the model generates tokens.
// Scan the HTTP response body and process line by line.
varvmap[string]interface{}scanner:=bufio.NewScanner(response.Body)forscanner.Scan(){line:=bytes.TrimSpace(scanner.Bytes())// Skip lines that don't start with "data: ".
if!bytes.HasPrefix(line,[]byte("data: ")){continue}// Remove the "data: " prefix.
payload:=bytes.TrimPrefix(line,[]byte("data: "))// If payload is "[DONE]", end streaming.
ifbytes.Equal(payload,[]byte("[DONE]")){break}// Parse the JSON data.
json.Unmarshal(payload,&v)// Extract the choices array from the response.
choices:=v["choices"].([]interface{})// Extract the first data.
choice:=choices[0].(map[string]interface{})// Extract the response token generated by the AI model.
text:=choice["text"]fmt.Println(text)}}
package main
import (
"bufio""bytes""encoding/json""fmt""net/http")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
// Define the data structure to be used for POST requests.
// Model: Model ID to use
// Prompt: Input text to provide to the AI
// Stream: Whether to stream response (optional)
type PostData struct {
Model string`json:"model"` Prompt string`json:"prompt"` Stream bool`json:"stream,omitempty"`}
funcmain() {
// Create request data.
// Set Stream: true to receive real-time streaming responses.
data := PostData{
Model: model,
Prompt: "Hi",
Stream: true,
}
// Marshal data to JSON format.
jsonData, err := json.Marshal(data)
if err !=nil {
panic(err)
}
// Send a POST request to the AIOS API's v1/completions endpoint.
response, err := http.Post(aiosBaseUrl+"/v1/completions", "application/json", bytes.NewBuffer(jsonData))
if err !=nil {
panic(err)
}
defer response.Body.Close()
// You can receive responses as the model generates tokens.
// Scan the HTTP response body and process line by line.
var v map[string]interface{}
scanner := bufio.NewScanner(response.Body)
for scanner.Scan() {
line := bytes.TrimSpace(scanner.Bytes())
// Skip lines that don't start with "data: ".
if !bytes.HasPrefix(line, []byte("data: ")) {
continue }
// Remove the "data: " prefix.
payload := bytes.TrimPrefix(line, []byte("data: "))
// If payload is "[DONE]", end streaming.
if bytes.Equal(payload, []byte("[DONE]")) {
break }
// Parse the JSON data.
json.Unmarshal(payload, &v)
// Extract the choices array from the response.
choices := v["choices"].([]interface{})
// Extract the first data.
choice := choices[0].(map[string]interface{})
// Extract the response token generated by the AI model.
text := choice["text"]
fmt.Println(text)
}
}
packagemainimport("context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option""github.com/openai/openai-go/packages/param")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)funcmain(){// Create an OpenAI client.
// Use option.WithBaseURL to set the v1 endpoint of the AIOS API.
client:=openai.NewClient(option.WithBaseURL(aiosBaseUrl+"/v1"),)// Generate a streaming completion using the AIOS model.
// Use openai.CompletionNewParams to set the model and prompt.
completion:=client.Completions.NewStreaming(context.TODO(),openai.CompletionNewParams{Model:openai.CompletionNewParamsModel(model),Prompt:openai.CompletionNewParamsPromptUnion{OfString:param.Opt[string]{Value:"Hi"}},})// You can receive responses as the model generates tokens.
// The Next() method returns true when there is a next chunk.
forcompletion.Next(){// Get the choices slice of the current chunk.
chunk:=completion.Current().Choices// choices[0].text is the response text generated by the AI model.
fmt.Println(chunk[0].Text)}}
package main
import (
"context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option""github.com/openai/openai-go/packages/param")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
funcmain() {
// Create an OpenAI client.
// Use option.WithBaseURL to set the v1 endpoint of the AIOS API.
client := openai.NewClient(
option.WithBaseURL(aiosBaseUrl +"/v1"),
)
// Generate a streaming completion using the AIOS model.
// Use openai.CompletionNewParams to set the model and prompt.
completion := client.Completions.NewStreaming(context.TODO(), openai.CompletionNewParams{
Model: openai.CompletionNewParamsModel(model),
Prompt: openai.CompletionNewParamsPromptUnion{OfString: param.Opt[string]{Value: "Hi"}},
})
// You can receive responses as the model generates tokens.
// The Next() method returns true when there is a next chunk.
for completion.Next() {
// Get the choices slice of the current chunk.
chunk := completion.Current().Choices
// choices[0].text is the response text generated by the AI model.
fmt.Println(chunk[0].Text)
}
}
Answers are generated for each token, and each token can be checked in the text field of choices.
I
'm
looking
for
a
way
to
check
if
a
specific
process
is
running
on
Chat Completion API
The Chat Completion API receives a list of messages (context) listed in order and generates an appropriate message as the next response.
non-stream request
Request
If the messages consist only of text messages, you can call them as follows.
Color mode
importjsonimportrequestsfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# This includes the model ID to use and the messages list.# The messages list includes system messages and user messages.data={"model":model,"messages":[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"Hi"}]}# Send a POST request to the AIOS API's v1/chat/completions endpoint.response=requests.post(urljoin(aios_base_url,"v1/chat/completions"),json=data)# Parse the response body in JSON format.body=json.loads(response.text)# choices[0].message is the response generated by the AI model.print(body["choices"][0]["message"])
importjsonimportrequestsfromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# This includes the model ID to use and the messages list.# The messages list includes system messages and user messages.data = {
"model": model,
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hi"}
]
}
# Send a POST request to the AIOS API's v1/chat/completions endpoint.response = requests.post(urljoin(aios_base_url, "v1/chat/completions"), json=data)
# Parse the response body in JSON format.body = json.loads(response.text)
# choices[0].message is the response generated by the AI model.print(body["choices"][0]["message"])
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")# Generate a chat completion using the AIOS model.# model parameter specifies the model ID to use.# messages parameter is a list of messages including system and user messages.response=client.chat.completions.create(model=model,messages=[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"Hi"}])# Output choices[0].message from the generated response.print(response.choices[0].message.model_dump())
fromopenaiimport OpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".client = OpenAI(base_url=urljoin(aios_base_url, "v1"), api_key="EMPTY_KEY")
# Generate a chat completion using the AIOS model.# model parameter specifies the model ID to use.# messages parameter is a list of messages including system and user messages.response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hi"}
]
)
# Output choices[0].message from the generated response.print(response.choices[0].message.model_dump())
fromlangchain_openaiimportChatOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".# model parameter specifies the model ID to use.chat_llm=ChatOpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY",model=model)# Configure the chat messages list.# Include system messages and user messages.messages=[("system","You are a helpful assistant."),("human","Hi"),]# Pass the messages list to the chat LLM and receive a response.# The invoke method returns the model's output.chat_completion=chat_llm.invoke(messages)# Output the generated response.print(chat_completion.model_dump())
fromlangchain_openaiimport ChatOpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".# model parameter specifies the model ID to use.chat_llm = ChatOpenAI(
base_url=urljoin(aios_base_url, "v1"),
api_key="EMPTY_KEY",
model=model
)
# Configure the chat messages list.# Include system messages and user messages.messages = [
("system", "You are a helpful assistant."),
("human", "Hi"),
]
# Pass the messages list to the chat LLM and receive a response.# The invoke method returns the model's output.chat_completion = chat_llm.invoke(messages)
# Output the generated response.print(chat_completion.model_dump())
constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Configure the request data.
// This includes the model ID to use and the messages list.
// The messages list includes system messages and user messages.
constdata={model:model,messages:[{role:"system",content:"You are a helpful assistant."},{role:"user",content:"Hi"},],};// Create the AIOS API's v1/chat/completions endpoint URL.
leturl=newURL("/v1/chat/completions",aios_base_url);// Send a POST request to the AIOS API.
constresponse=awaitfetch(url,{method:"POST",headers:{"Content-Type":"application/json",},body:JSON.stringify(data),});// Parse the response body in JSON format.
constbody=awaitresponse.json();// Output choices[0].message from the generated response.
console.log(body.choices[0].message);
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Configure the request data.
// This includes the model ID to use and the messages list.
// The messages list includes system messages and user messages.
const data = {
model: model,
messages: [
{ role:"system", content:"You are a helpful assistant." },
{ role:"user", content:"Hi" },
],
};
// Create the AIOS API's v1/chat/completions endpoint URL.
let url =new URL("/v1/chat/completions", aios_base_url);
// Send a POST request to the AIOS API.
const response =await fetch(url, {
method:"POST",
headers: {
"Content-Type":"application/json",
},
body: JSON.stringify(data),
});
// Parse the response body in JSON format.
const body =await response.json();
// Output choices[0].message from the generated response.
console.log(body.choices[0].message);
importOpenAIfrom"openai";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
constclient=newOpenAI({apiKey:"EMPTY_KEY",baseURL:newURL("v1",aios_base_url).href,});// Generate a chat completion using the AIOS model.
// model parameter specifies the model ID to use.
// messages parameter is a list of messages including system and user messages.
constresponse=awaitclient.chat.completions.create({model:model,messages:[{role:"system",content:"You are a helpful assistant."},{role:"user",content:"Hi"},],});// Output choices[0].message from the generated response.
console.log(response.choices[0].message);
import OpenAI from "openai";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
const client =new OpenAI({
apiKey:"EMPTY_KEY",
baseURL:new URL("v1", aios_base_url).href,
});
// Generate a chat completion using the AIOS model.
// model parameter specifies the model ID to use.
// messages parameter is a list of messages including system and user messages.
const response =await client.chat.completions.create({
model: model,
messages: [
{ role:"system", content:"You are a helpful assistant." },
{ role:"user", content:"Hi" },
],
});
// Output choices[0].message from the generated response.
console.log(response.choices[0].message);
import{HumanMessage,SystemMessage}from"@langchain/core/messages";import{ChatOpenAI}from"@langchain/openai";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.
// model parameter specifies the model ID to use.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// configuration.baseURL points to the v1 endpoint of the AIOS API.
constllm=newChatOpenAI({model:model,apiKey:"EMPTY_KEY",configuration:{baseURL:newURL("v1",aios_base_url).href,},});// Configure the chat messages list.
// Include system messages and user messages using SystemMessage and HumanMessage objects.
constmessages=[newSystemMessage("You are a helpful assistant."),newHumanMessage("Hi"),];// Pass the messages list to the chat LLM and receive a response.
// The invoke method returns the model's output.
constresponse=awaitllm.invoke(messages);// Output the content of the generated response.
// This content is the response text generated by the AI model.
console.log(response.content);
import { HumanMessage, SystemMessage } from "@langchain/core/messages";
import { ChatOpenAI } from "@langchain/openai";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.
// model parameter specifies the model ID to use.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// configuration.baseURL points to the v1 endpoint of the AIOS API.
const llm =new ChatOpenAI({
model: model,
apiKey:"EMPTY_KEY",
configuration: {
baseURL:new URL("v1", aios_base_url).href,
},
});
// Configure the chat messages list.
// Include system messages and user messages using SystemMessage and HumanMessage objects.
const messages = [
new SystemMessage("You are a helpful assistant."),
new HumanMessage("Hi"),
];
// Pass the messages list to the chat LLM and receive a response.
// The invoke method returns the model's output.
const response =await llm.invoke(messages);
// Output the content of the generated response.
// This content is the response text generated by the AI model.
console.log(response.content);
packagemainimport("bytes""encoding/json""fmt""io""net/http")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)// Define the message structure.
// Role: Message role (e.g., system, user)
// Content: Message content
typeMessagestruct{Rolestring`json:"role"`Contentstring`json:"content"`}// Define the data structure to be used for POST requests.
// Model: Model ID to use
// Messages: List of messages
// Stream: Whether to stream response (optional)
typePostDatastruct{Modelstring`json:"model"`Messages[]Message`json:"messages"`Streambool`json:"stream,omitempty"`}funcmain(){// Create request data.
// The messages list includes system messages and user messages.
data:=PostData{Model:model,Messages:[]Message{{Role:"system",Content:"You are a helpful assistant.",},{Role:"user",Content:"Hi",},},}// Marshal data to JSON format.
jsonData,err:=json.Marshal(data)iferr!=nil{panic(err)}// Send a POST request to the AIOS API's v1/chat/completions endpoint.
response,err:=http.Post(aiosBaseUrl+"/v1/chat/completions","application/json",bytes.NewBuffer(jsonData))iferr!=nil{panic(err)}deferresponse.Body.Close()// Read the entire response body.
body,err:=io.ReadAll(response.Body)iferr!=nil{panic(err)}// Unmarshal the response body into map format.
varvmap[string]interface{}json.Unmarshal(body,&v)// Extract the choices array from the response.
choices:=v["choices"].([]interface{})// Extract the first data.
choice:=choices[0].(map[string]interface{})// Format and output the response message generated by the AI model in JSON format.
message,err:=json.MarshalIndent(choice["message"],""," ")iferr!=nil{panic(err)}fmt.Println(string(message))}
package main
import (
"bytes""encoding/json""fmt""io""net/http")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
// Define the message structure.
// Role: Message role (e.g., system, user)
// Content: Message content
type Message struct {
Role string`json:"role"` Content string`json:"content"`}
// Define the data structure to be used for POST requests.
// Model: Model ID to use
// Messages: List of messages
// Stream: Whether to stream response (optional)
type PostData struct {
Model string`json:"model"` Messages []Message `json:"messages"` Stream bool`json:"stream,omitempty"`}
funcmain() {
// Create request data.
// The messages list includes system messages and user messages.
data := PostData{
Model: model,
Messages: []Message{
{
Role: "system",
Content: "You are a helpful assistant.",
},
{
Role: "user",
Content: "Hi",
},
},
}
// Marshal data to JSON format.
jsonData, err := json.Marshal(data)
if err !=nil {
panic(err)
}
// Send a POST request to the AIOS API's v1/chat/completions endpoint.
response, err := http.Post(aiosBaseUrl+"/v1/chat/completions", "application/json", bytes.NewBuffer(jsonData))
if err !=nil {
panic(err)
}
defer response.Body.Close()
// Read the entire response body.
body, err := io.ReadAll(response.Body)
if err !=nil {
panic(err)
}
// Unmarshal the response body into map format.
var v map[string]interface{}
json.Unmarshal(body, &v)
// Extract the choices array from the response.
choices := v["choices"].([]interface{})
// Extract the first data.
choice := choices[0].(map[string]interface{})
// Format and output the response message generated by the AI model in JSON format.
message, err := json.MarshalIndent(choice["message"], "", " ")
if err !=nil {
panic(err)
}
fmt.Println(string(message))
}
packagemainimport("context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)funcmain(){// Create an OpenAI client.
// Use option.WithBaseURL to set the v1 endpoint of the AIOS API.
client:=openai.NewClient(option.WithBaseURL(aiosBaseUrl+"/v1"),)// Generate a chat completion using the AIOS model.
// Use openai.ChatCompletionNewParams to set the model and messages list.
// The messages list includes system messages and user messages.
response,err:=client.Chat.Completions.New(context.TODO(),openai.ChatCompletionNewParams{Model:model,Messages:[]openai.ChatCompletionMessageParamUnion{openai.SystemMessage("You are a helpful assistant."),openai.UserMessage("Hi"),},})iferr!=nil{panic(err)}// Format and output the response message generated by the AI model in JSON format.
fmt.Println(response.Choices[0].Message.RawJSON())}
package main
import (
"context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
funcmain() {
// Create an OpenAI client.
// Use option.WithBaseURL to set the v1 endpoint of the AIOS API.
client := openai.NewClient(
option.WithBaseURL(aiosBaseUrl +"/v1"),
)
// Generate a chat completion using the AIOS model.
// Use openai.ChatCompletionNewParams to set the model and messages list.
// The messages list includes system messages and user messages.
response, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
Model: model,
Messages: []openai.ChatCompletionMessageParamUnion{
openai.SystemMessage("You are a helpful assistant."),
openai.UserMessage("Hi"),
},
})
if err !=nil {
panic(err)
}
// Format and output the response message generated by the AI model in JSON format.
fmt.Println(response.Choices[0].Message.RawJSON())
}
Code Block. /v1/chat/completions request
Note
For information on the aios endpoint-url and model ID for calling the model, see the LLM Endpoint Usage Guide on the resource details page. Please refer to Using LLM.
Response
You can check the model’s answer content in message of choices.
{'annotations':None,'audio':None,'content':'Hello! How can I help you today?','function_call':None,'reasoning_content':'The user says "Hi". We respond politely.','refusal':None,'role':'assistant','tool_calls':[]}
stream request
Using stream, you can receive and process responses for each token the model generates, instead of waiting for the model to generate all answers and receiving them at once.
Request
Enter True for the stream parameter value.
Color mode
importjsonimportrequestsfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# This includes the model ID to use, the messages list, and whether to stream.# The messages list includes system messages and user messages.data={"model":model,"messages":[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"Hi"}],"stream":True}# Send a POST request to the AIOS API's v1/chat/completions endpoint.# Set stream=True to receive real-time streaming responses.response=requests.post(urljoin(aios_base_url,"v1/chat/completions"),json=data,stream=True)# You can receive responses as the model generates tokens.# Responses are sent separated by each line, so process with iter_lines().forlineinresponse.iter_lines():ifline:try:# Remove the 'data: ' prefix and parse the JSON data.body=json.loads(line[len("data: "):])# Output the delta (choices[0].delta).# The delta is the response token generated by the AI model.print(body["choices"][0]["delta"])except:pass
importjsonimportrequestsfromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# This includes the model ID to use, the messages list, and whether to stream.# The messages list includes system messages and user messages.data = {
"model": model,
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hi"}
],
"stream": True}
# Send a POST request to the AIOS API's v1/chat/completions endpoint.# Set stream=True to receive real-time streaming responses.response = requests.post(urljoin(aios_base_url, "v1/chat/completions"), json=data, stream=True)
# You can receive responses as the model generates tokens.# Responses are sent separated by each line, so process with iter_lines().for line in response.iter_lines():
if line:
try:
# Remove the 'data: ' prefix and parse the JSON data. body = json.loads(line[len("data: "):])
# Output the delta (choices[0].delta).# The delta is the response token generated by the AI model. print(body["choices"][0]["delta"])
except:
pass
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")# Generate a chat completion using the AIOS model.# model parameter specifies the model ID to use.# messages parameter is a list of messages including system and user messages.# Set stream=True to receive real-time streaming responses.response=client.chat.completions.create(model=model,messages=[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"Hi"}],stream=True)# You can receive responses as the model generates tokens.# response is sent in stream format, so you can process it iteratively.forchunkinresponse:# Output the delta (choices[0].delta).# The delta is the response token generated by the AI model.print(chunk.choices[0].delta.model_dump())
fromopenaiimport OpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".client = OpenAI(base_url=urljoin(aios_base_url, "v1"), api_key="EMPTY_KEY")
# Generate a chat completion using the AIOS model.# model parameter specifies the model ID to use.# messages parameter is a list of messages including system and user messages.# Set stream=True to receive real-time streaming responses.response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hi"}
],
stream=True)
# You can receive responses as the model generates tokens.# response is sent in stream format, so you can process it iteratively.for chunk in response:
# Output the delta (choices[0].delta).# The delta is the response token generated by the AI model. print(chunk.choices[0].delta.model_dump())
fromlangchain_openaiimportChatOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".# model parameter specifies the model ID to use.llm=ChatOpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY",model=model)# Configure the chat messages list.# Include system messages and user messages.messages=[("system","You are a helpful assistant."),("human","Hi"),]# You can receive responses as the model generates tokens.# The llm.stream method returns a stream that generates tokens in real-time.forchunkinllm.stream(messages):# Output each chunk.# This chunk is the response token generated by the AI model.print(chunk)
fromlangchain_openaiimport ChatOpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.# base_url points to the v1 endpoint of the AIOS API,# api_key is the key required by AIOS, typically set to "EMPTY_KEY".# model parameter specifies the model ID to use.llm = ChatOpenAI(
base_url=urljoin(aios_base_url, "v1"),
api_key="EMPTY_KEY",
model=model
)
# Configure the chat messages list.# Include system messages and user messages.messages = [
("system", "You are a helpful assistant."),
("human", "Hi"),
]
# You can receive responses as the model generates tokens.# The llm.stream method returns a stream that generates tokens in real-time.for chunk in llm.stream(messages):
# Output each chunk.# This chunk is the response token generated by the AI model. print(chunk)
constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Configure the request data.
// This includes the model ID to use, the messages list, and whether to stream.
// The messages list includes system messages and user messages.
constdata={model:model,messages:[{role:"system",content:"You are a helpful assistant."},{role:"user",content:"Hi"},],stream:true,};// Create the AIOS API's v1/chat/completions endpoint URL.
leturl=newURL("/v1/chat/completions",aios_base_url);// Send a POST request to the AIOS API.
constresponse=awaitfetch(url,{method:"POST",headers:{"Content-Type":"application/json",},body:JSON.stringify(data),});// You can receive responses as the model generates tokens.
// Convert the response body to a text decoder stream and read it.
constreader=response.body.pipeThrough(newTextDecoderStream()).getReader();letbuf="";while(true){const{value,done}=awaitreader.read();if(done)break;// Add received data to buffer.
buf+=value;letsep;// Find newline characters (\n\n) in the buffer and separate data.
while((sep=buf.indexOf("\n\n"))>=0){constdata=buf.slice(0,sep);buf=buf.slice(sep+2);// Process each line.
for(constrawLineofdata.split("\n")){constline=rawLine.trim();if(!line.startsWith("data: "))continue;// Remove the "data: " prefix and extract JSON data.
constpayload=line.slice("data: ".length).trim();if(payload==="[DONE]")break;// Parse the JSON data.
constjson=JSON.parse(payload);// Output the delta (choices[0].delta).
// The delta is the response token generated by the AI model.
console.log(json.choices[0].delta);}}}
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Configure the request data.
// This includes the model ID to use, the messages list, and whether to stream.
// The messages list includes system messages and user messages.
const data = {
model: model,
messages: [
{ role:"system", content:"You are a helpful assistant." },
{ role:"user", content:"Hi" },
],
stream:true,
};
// Create the AIOS API's v1/chat/completions endpoint URL.
let url =new URL("/v1/chat/completions", aios_base_url);
// Send a POST request to the AIOS API.
const response =await fetch(url, {
method:"POST",
headers: {
"Content-Type":"application/json",
},
body: JSON.stringify(data),
});
// You can receive responses as the model generates tokens.
// Convert the response body to a text decoder stream and read it.
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
let buf ="";
while (true) {
const { value, done } =await reader.read();
if (done) break;
// Add received data to buffer.
buf += value;
let sep;
// Find newline characters (\n\n) in the buffer and separate data.
while ((sep = buf.indexOf("\n\n")) >=0) {
const data = buf.slice(0, sep);
buf = buf.slice(sep +2);
// Process each line.
for (const rawLine of data.split("\n")) {
const line = rawLine.trim();
if (!line.startsWith("data: ")) continue;
// Remove the "data: " prefix and extract JSON data.
const payload = line.slice("data: ".length).trim();
if (payload ==="[DONE]") break;
// Parse the JSON data.
const json = JSON.parse(payload);
// Output the delta (choices[0].delta).
// The delta is the response token generated by the AI model.
console.log(json.choices[0].delta);
}
}
}
importOpenAIfrom"openai";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
constclient=newOpenAI({apiKey:"EMPTY_KEY",baseURL:newURL("v1",aios_base_url).href,});// Generate a chat completion using the AIOS model.
// model parameter specifies the model ID to use.
// messages parameter is a list of messages including system and user messages.
// Set stream: true to receive real-time streaming responses.
constresponse=awaitclient.chat.completions.create({model:model,messages:[{role:"system",content:"You are a helpful assistant."},{role:"user",content:"Hi"},],stream:true,});// You can receive responses as the model generates tokens.
// Use for await...of loop to sequentially process stream events.
forawait(consteventofresponse){// Output the delta (choices[0].delta).
// The delta is the response token generated by the AI model.
console.log(event.choices[0].delta);}
import OpenAI from "openai";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
const client =new OpenAI({
apiKey:"EMPTY_KEY",
baseURL:new URL("v1", aios_base_url).href,
});
// Generate a chat completion using the AIOS model.
// model parameter specifies the model ID to use.
// messages parameter is a list of messages including system and user messages.
// Set stream: true to receive real-time streaming responses.
const response =await client.chat.completions.create({
model: model,
messages: [
{ role:"system", content:"You are a helpful assistant." },
{ role:"user", content:"Hi" },
],
stream:true,
});
// You can receive responses as the model generates tokens.
// Use for await...of loop to sequentially process stream events.
forawait (const event of response) {
// Output the delta (choices[0].delta).
// The delta is the response token generated by the AI model.
console.log(event.choices[0].delta);
}
import{ChatOpenAI}from"@langchain/openai";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.
// model parameter specifies the model ID to use.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// configuration.baseURL points to the v1 endpoint of the AIOS API.
constllm=newChatOpenAI({model:model,apiKey:"EMPTY_KEY",configuration:{baseURL:newURL("v1",aios_base_url).href,},});// Configure the chat messages list.
// Include system messages and user messages.
constmessages=[{role:"system",content:"You are a helpful assistant."},{role:"user",content:"Hi"},];// You can receive responses as the model generates tokens.
// The llm.stream method returns a stream that generates tokens in real-time.
constcompletion=awaitllm.stream(messages);forawait(constchunkofcompletion){// Output the content of each chunk.
// This content is the response token generated by the AI model.
console.log(chunk.content);}
import { ChatOpenAI } from "@langchain/openai";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.
// model parameter specifies the model ID to use.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// configuration.baseURL points to the v1 endpoint of the AIOS API.
const llm =new ChatOpenAI({
model: model,
apiKey:"EMPTY_KEY",
configuration: {
baseURL:new URL("v1", aios_base_url).href,
},
});
// Configure the chat messages list.
// Include system messages and user messages.
const messages = [
{ role:"system", content:"You are a helpful assistant." },
{ role:"user", content:"Hi" },
];
// You can receive responses as the model generates tokens.
// The llm.stream method returns a stream that generates tokens in real-time.
const completion =await llm.stream(messages);
forawait (const chunk of completion) {
// Output the content of each chunk.
// This content is the response token generated by the AI model.
console.log(chunk.content);
}
packagemainimport("bufio""bytes""encoding/json""fmt""net/http")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)// Define the message structure.
// Role: Message role (e.g., system, user)
// Content: Message content
typeMessagestruct{Rolestring`json:"role"`Contentstring`json:"content"`}// Define the data structure to be used for POST requests.
// Model: Model ID to use
// Messages: List of messages
// Stream: Whether to stream response (optional)
typePostDatastruct{Modelstring`json:"model"`Messages[]Message`json:"messages"`Streambool`json:"stream,omitempty"`}funcmain(){// Create request data.
// The messages list includes system messages and user messages.
// Set Stream: true to receive real-time streaming responses.
data:=PostData{Model:model,Messages:[]Message{{Role:"system",Content:"You are a helpful assistant.",},{Role:"user",Content:"Hi",},},Stream:true,}// Marshal data to JSON format.
jsonData,err:=json.Marshal(data)iferr!=nil{panic(err)}// Send a POST request to the AIOS API's v1/chat/completions endpoint.
response,err:=http.Post(aiosBaseUrl+"/v1/chat/completions","application/json",bytes.NewBuffer(jsonData))iferr!=nil{panic(err)}deferresponse.Body.Close()// You can receive responses as the model generates tokens.
// Scan the HTTP response body and process line by line.
varvmap[string]interface{}scanner:=bufio.NewScanner(response.Body)forscanner.Scan(){line:=bytes.TrimSpace(scanner.Bytes())// Skip lines that don't start with "data: ".
if!bytes.HasPrefix(line,[]byte("data: ")){continue}// Remove the "data: " prefix.
payload:=bytes.TrimPrefix(line,[]byte("data: "))// If payload is "[DONE]", end streaming.
ifbytes.Equal(payload,[]byte("[DONE]")){break}// Parse the JSON data.
json.Unmarshal(payload,&v)// Extract the choices array from the response.
choices:=v["choices"].([]interface{})// Extract the first data.
choice:=choices[0].(map[string]interface{})// Serialize the delta to JSON format and output it.
message,err:=json.Marshal(choice["delta"])iferr!=nil{panic(err)}fmt.Println(string(message))}}
package main
import (
"bufio""bytes""encoding/json""fmt""net/http")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
// Define the message structure.
// Role: Message role (e.g., system, user)
// Content: Message content
type Message struct {
Role string`json:"role"` Content string`json:"content"`}
// Define the data structure to be used for POST requests.
// Model: Model ID to use
// Messages: List of messages
// Stream: Whether to stream response (optional)
type PostData struct {
Model string`json:"model"` Messages []Message `json:"messages"` Stream bool`json:"stream,omitempty"`}
funcmain() {
// Create request data.
// The messages list includes system messages and user messages.
// Set Stream: true to receive real-time streaming responses.
data := PostData{
Model: model,
Messages: []Message{
{
Role: "system",
Content: "You are a helpful assistant.",
},
{
Role: "user",
Content: "Hi",
},
},
Stream: true,
}
// Marshal data to JSON format.
jsonData, err := json.Marshal(data)
if err !=nil {
panic(err)
}
// Send a POST request to the AIOS API's v1/chat/completions endpoint.
response, err := http.Post(aiosBaseUrl+"/v1/chat/completions", "application/json", bytes.NewBuffer(jsonData))
if err !=nil {
panic(err)
}
defer response.Body.Close()
// You can receive responses as the model generates tokens.
// Scan the HTTP response body and process line by line.
var v map[string]interface{}
scanner := bufio.NewScanner(response.Body)
for scanner.Scan() {
line := bytes.TrimSpace(scanner.Bytes())
// Skip lines that don't start with "data: ".
if !bytes.HasPrefix(line, []byte("data: ")) {
continue }
// Remove the "data: " prefix.
payload := bytes.TrimPrefix(line, []byte("data: "))
// If payload is "[DONE]", end streaming.
if bytes.Equal(payload, []byte("[DONE]")) {
break }
// Parse the JSON data.
json.Unmarshal(payload, &v)
// Extract the choices array from the response.
choices := v["choices"].([]interface{})
// Extract the first data.
choice := choices[0].(map[string]interface{})
// Serialize the delta to JSON format and output it.
message, err := json.Marshal(choice["delta"])
if err !=nil {
panic(err)
}
fmt.Println(string(message))
}
}
packagemainimport("context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)funcmain(){// Create an OpenAI client.
// Use option.WithBaseURL to set the v1 endpoint of the AIOS API.
client:=openai.NewClient(option.WithBaseURL(aiosBaseUrl+"/v1"),)// Generate a streaming chat completion using the AIOS model.
// Use openai.ChatCompletionNewParams to set the model and messages list.
completion:=client.Chat.Completions.NewStreaming(context.TODO(),openai.ChatCompletionNewParams{Model:model,Messages:[]openai.ChatCompletionMessageParamUnion{openai.SystemMessage("You are a helpful assistant."),openai.UserMessage("Hi"),},})// You can receive responses as the model generates tokens.
// The Next() method returns true when there is a next chunk.
forcompletion.Next(){// Get the choices slice of the current chunk.
chunk:=completion.Current().Choices// choices[0].text is the response text generated by the AI model.
fmt.Println(chunk[0].Message.Content)}}
package main
import (
"context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
funcmain() {
// Create an OpenAI client.
// Use option.WithBaseURL to set the v1 endpoint of the AIOS API.
client := openai.NewClient(
option.WithBaseURL(aiosBaseUrl +"/v1"),
)
// Generate a streaming chat completion using the AIOS model.
// Use openai.ChatCompletionNewParams to set the model and messages list.
completion := client.Chat.Completions.NewStreaming(context.TODO(), openai.ChatCompletionNewParams{
Model: model,
Messages: []openai.ChatCompletionMessageParamUnion{
openai.SystemMessage("You are a helpful assistant."),
openai.UserMessage("Hi"),
},
})
// You can receive responses as the model generates tokens.
// The Next() method returns true when there is a next chunk.
for completion.Next() {
// Get the choices slice of the current chunk.
chunk := completion.Current().Choices
// choices[0].text is the response text generated by the AI model.
fmt.Println(chunk[0].Message.Content)
}
}
Answers are generated for each token, and each token can be checked in the text field of choices.
I
'm
looking
for
a
way
to
check
if
a
specific
process
is
running
on
}
tool calling
Tool Calling allows the model to call external functions to perform specific tasks.
The model analyzes the user’s request, selects the necessary tools, and generates the arguments as a response to call those tools.
After executing the actual tool using the tool call message generated by the model, you compose the result as a tool message and request the model again, then the model generates a natural response to the user based on the tool execution results.
Fig. tool calling sequence diagram
Note
The openai/gpt-oss-120b model does not support the tool calling feature.
Request
Color mode
importjsonimportrequestsfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Define a function to get weather information.# This function returns the current temperature in Celsius for the provided coordinates.tools=[{"type":"function","function":{"name":"get_weather","description":"Get current temperature for provided coordinates in celsius.","parameters":{"type":"object","properties":{"latitude":{"type":"number"},"longitude":{"type":"number"}},"required":["latitude","longitude"],"additionalProperties":False},"strict":True}}]# Define the user message.# The user is asking about today's weather in Paris.messages=[{"role":"user","content":"What is the weather like in Paris today?"}]# Configure the request data.# This includes the model ID to use, the messages list, and the tools list.data={"model":model,"messages":messages,"tools":tools}# Send a POST request to the AIOS API's v1/chat/completions endpoint.# This request instructs the model to process the user's question and determine the necessary tool calls.response=requests.post(urljoin(aios_base_url,"v1/chat/completions"),json=data)# Parse the response body in JSON format.body=json.loads(response.text)# Print the tool call information from the response generated by the AI model.# This information indicates which tool the model should call.print(body["choices"][0]["message"]["tool_calls"])# Implementation of the weather function, always returns 14 degrees.defget_weather(latitude,longitude):return"14℃"# Extract tool call information from the first response.# This retrieves the tool call information requested by the model.tool_call=body["choices"][0]["message"]["tool_calls"][0]# Parse the arguments of the tool call from JSON string format to dict format.args=json.loads(tool_call["function"]["arguments"])# Call the actual function to get the result. (e.g., "14℃")# At this step, the actual weather information lookup logic is executed.result=get_weather(args["latitude"],args["longitude"])# "14℃"# Add the function call result as a **tool** message to the conversation context and call the model again,# then the model generates an appropriate response using the function call result.# Add the model's tool call message to messages to maintain the conversation context.messages.append(body["choices"][0]["message"])# Add the result of calling the actual function to messages.# This allows the model to generate a final response based on the tool call result.messages.append({"role":"tool","tool_call_id":tool_call["id"],"content":str(result)})# Configure the second request data.# This includes the model ID to use and the updated messages list.data={"model":model,"messages":messages,}# Send a POST request to the AIOS API's v1/chat/completions endpoint.# This request generates a final response based on the tool call result.response_2=requests.post(urljoin(aios_base_url,"v1/chat/completions"),json=data)# Parse the response body in JSON format.body=json.loads(response_2.text)# Print the message generated by the AI in the second response.# This is the final answer to the user's question.print(body["choices"][0]["message"])
importjsonimportrequestsfromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Define a function to get weather information.# This function returns the current temperature in Celsius for the provided coordinates.tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current temperature for provided coordinates in celsius.",
"parameters": {
"type": "object",
"properties": {
"latitude": {"type": "number"},
"longitude": {"type": "number"}
},
"required": ["latitude", "longitude"],
"additionalProperties": False },
"strict": True }
}]
# Define the user message.# The user is asking about today's weather in Paris.messages = [{"role": "user", "content": "What is the weather like in Paris today?"}]
# Configure the request data.# This includes the model ID to use, the messages list, and the tools list.data = {
"model": model,
"messages": messages,
"tools": tools
}
# Send a POST request to the AIOS API's v1/chat/completions endpoint.# This request instructs the model to process the user's question and determine the necessary tool calls.response = requests.post(urljoin(aios_base_url, "v1/chat/completions"), json=data)
# Parse the response body in JSON format.body = json.loads(response.text)
# Print the tool call information from the response generated by the AI model.# This information indicates which tool the model should call.print(body["choices"][0]["message"]["tool_calls"])
# Implementation of the weather function, always returns 14 degrees.defget_weather(latitude, longitude):
return"14℃"# Extract tool call information from the first response.# This retrieves the tool call information requested by the model.tool_call = body["choices"][0]["message"]["tool_calls"][0]
# Parse the arguments of the tool call from JSON string format to dict format.args = json.loads(tool_call["function"]["arguments"])
# Call the actual function to get the result. (e.g., "14℃")# At this step, the actual weather information lookup logic is executed.result = get_weather(args["latitude"], args["longitude"]) # "14℃"# Add the function call result as a **tool** message to the conversation context and call the model again,# then the model generates an appropriate response using the function call result.# Add the model's tool call message to messages to maintain the conversation context.messages.append(body["choices"][0]["message"])
# Add the result of calling the actual function to messages.# This allows the model to generate a final response based on the tool call result.messages.append({
"role": "tool",
"tool_call_id": tool_call["id"],
"content": str(result)
})
# Configure the second request data.# This includes the model ID to use and the updated messages list.data = {
"model": model,
"messages": messages,
}
# Send a POST request to the AIOS API's v1/chat/completions endpoint.# This request generates a final response based on the tool call result.response_2 = requests.post(urljoin(aios_base_url, "v1/chat/completions"), json=data)
# Parse the response body in JSON format.body = json.loads(response_2.text)
# Print the message generated by the AI in the second response.# This is the final answer to the user's question.print(body["choices"][0]["message"])
importjsonfromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")# Define a function to get weather information.# This function returns the current temperature in Celsius for the provided coordinates.tools=[{"type":"function","function":{"name":"get_weather","description":"Get current temperature for provided coordinates in celsius.","parameters":{"type":"object","properties":{"latitude":{"type":"number"},"longitude":{"type":"number"}},"required":["latitude","longitude"],"additionalProperties":False},"strict":True}}]# Define the user message.# The user is asking about today's weather in Paris.messages=[{"role":"user","content":"What is the weather like in Paris today?"}]# Generate a chat completion using the AIOS model.# The model parameter specifies the model ID to use.# The messages parameter is a list of messages containing the user message.# The tools parameter provides metadata about the tools available to the model.response=client.chat.completions.create(model=model,messages=messages,tools=tools# Provides metadata about the tools available to the model.)# Print the tool call information from the response generated by the AI model.# This information indicates which tool the model should call.print(response.choices[0].message.tool_calls[0].model_dump())# Implementation of the weather function, always returns 14 degrees.defget_weather(latitude,longitude):return"14℃"# Extract tool call information from the first response.# This retrieves the tool call information requested by the model.tool_call=response.choices[0].message.tool_calls[0]# Parse the tool call arguments in JSON format.args=json.loads(tool_call.function.arguments)# Call the actual function to get the result. (e.g., "14℃")# At this step, the actual weather information lookup logic is executed.result=get_weather(args["latitude"],args["longitude"])# "14℃"# Add the function call result as a **tool** message to the conversation context and call the model again,# then the model generates an appropriate response using the function call result.# Add the model's tool call message to messages to maintain the conversation context.messages.append(response.choices[0].message)# Add the result of calling the actual function to messages.# This allows the model to generate a final response based on the tool call result.messages.append({"role":"tool","tool_call_id":tool_call.id,"content":str(result)})# Generate a second chat completion.# This includes the model ID to use and the updated messages list.# This request generates a final response based on the tool call result.response_2=client.chat.completions.create(model=model,messages=messages,)# Print the message generated by the AI in the second response.# This is the final answer to the user's question.print(response_2.choices[0].message.model_dump())
importjsonfromopenaiimport OpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".client = OpenAI(base_url=urljoin(aios_base_url, "v1"), api_key="EMPTY_KEY")
# Define a function to get weather information.# This function returns the current temperature in Celsius for the provided coordinates.tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current temperature for provided coordinates in celsius.",
"parameters": {
"type": "object",
"properties": {
"latitude": {"type": "number"},
"longitude": {"type": "number"}
},
"required": ["latitude", "longitude"],
"additionalProperties": False },
"strict": True }
}]
# Define the user message.# The user is asking about today's weather in Paris.messages = [{"role": "user", "content": "What is the weather like in Paris today?"}]
# Generate a chat completion using the AIOS model.# The model parameter specifies the model ID to use.# The messages parameter is a list of messages containing the user message.# The tools parameter provides metadata about the tools available to the model.response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools # Provides metadata about the tools available to the model.)
# Print the tool call information from the response generated by the AI model.# This information indicates which tool the model should call.print(response.choices[0].message.tool_calls[0].model_dump())
# Implementation of the weather function, always returns 14 degrees.defget_weather(latitude, longitude):
return"14℃"# Extract tool call information from the first response.# This retrieves the tool call information requested by the model.tool_call = response.choices[0].message.tool_calls[0]
# Parse the tool call arguments in JSON format.args = json.loads(tool_call.function.arguments)
# Call the actual function to get the result. (e.g., "14℃")# At this step, the actual weather information lookup logic is executed.result = get_weather(args["latitude"], args["longitude"]) # "14℃"# Add the function call result as a **tool** message to the conversation context and call the model again,# then the model generates an appropriate response using the function call result.# Add the model's tool call message to messages to maintain the conversation context.messages.append(response.choices[0].message)
# Add the result of calling the actual function to messages.# This allows the model to generate a final response based on the tool call result.messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": str(result)
})
# Generate a second chat completion.# This includes the model ID to use and the updated messages list.# This request generates a final response based on the tool call result.response_2 = client.chat.completions.create(
model=model,
messages=messages,
)
# Print the message generated by the AI in the second response.# This is the final answer to the user's question.print(response_2.choices[0].message.model_dump())
fromlangchain_openaiimportChatOpenAIfromlangchain_core.toolsimporttoolfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Define a tool function to get weather information.# This function returns the current temperature in Celsius for the provided coordinates.@tooldefget_weather(latitude:float,longitude:float)->str:"""Get current temperature for provided coordinates in celsius."""return"14℃"# Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".# The model parameter specifies the model ID to use.chat_llm=ChatOpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY",model=model)# Bind tools to the model.# The get_weather function returns the current temperature in Celsius for the provided coordinates.llm_with_tools=chat_llm.bind_tools([get_weather])# Configure the list of chat messages.# The user is asking about today's weather in Paris.messages=[("human","What is the weather like in Paris today?")]# Pass the list of messages to the chat LLM to get a response.# The invoke method returns the model's output.# At this step, the model analyzes the user's question and determines the necessary tool calls.response=llm_with_tools.invoke(messages)# Print the tool call information from the response generated by the AI model.# This information indicates which tool the model should call.print(response.tool_calls)# Add the model's tool call message to messages to maintain the conversation context.# This allows the model to remember and connect previous conversation content.messages.append(response)# Call the actual tool function to get the result.# At this step, the get_weather function is executed to return weather information.tool_call=response.tool_calls[0]tool_message=get_weather.invoke(tool_call)# Add the tool call result to messages.# This allows the model to generate a final response based on the tool call result.messages.append(tool_message)# Perform a second request to get the final answer.# Now the model generates an appropriate response to the user based on the tool call result.response2=chat_llm.invoke(messages)# Print the final AI model response.# This is the final answer to the user's question.print(response2.model_dump())
fromlangchain_openaiimport ChatOpenAI
fromlangchain_core.toolsimport tool
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Define a tool function to get weather information.# This function returns the current temperature in Celsius for the provided coordinates.@tooldefget_weather(latitude: float, longitude: float) -> str:
"""Get current temperature for provided coordinates in celsius."""return"14℃"# Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".# The model parameter specifies the model ID to use.chat_llm = ChatOpenAI(
base_url=urljoin(aios_base_url, "v1"),
api_key="EMPTY_KEY",
model=model
)
# Bind tools to the model.# The get_weather function returns the current temperature in Celsius for the provided coordinates.llm_with_tools = chat_llm.bind_tools([get_weather])
# Configure the list of chat messages.# The user is asking about today's weather in Paris.messages = [("human", "What is the weather like in Paris today?")]
# Pass the list of messages to the chat LLM to get a response.# The invoke method returns the model's output.# At this step, the model analyzes the user's question and determines the necessary tool calls.response = llm_with_tools.invoke(messages)
# Print the tool call information from the response generated by the AI model.# This information indicates which tool the model should call.print(response.tool_calls)
# Add the model's tool call message to messages to maintain the conversation context.# This allows the model to remember and connect previous conversation content.messages.append(response)
# Call the actual tool function to get the result.# At this step, the get_weather function is executed to return weather information.tool_call = response.tool_calls[0]
tool_message = get_weather.invoke(tool_call)
# Add the tool call result to messages.# This allows the model to generate a final response based on the tool call result.messages.append(tool_message)
# Perform a second request to get the final answer.# Now the model generates an appropriate response to the user based on the tool call result.response2 = chat_llm.invoke(messages)
# Print the final AI model response.# This is the final answer to the user's question.print(response2.model_dump())
constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Define a function to get weather information.
// This function returns the current temperature in Celsius for the provided coordinates.
consttools=[{type:"function",function:{name:"get_weather",description:"Get current temperature for provided coordinates in celsius.",parameters:{type:"object",properties:{latitude:{type:"number"},longitude:{type:"number"},},required:["latitude","longitude"],additionalProperties:false,},strict:true,},},];// Define the user message.
// The user is asking about today's weather in Paris.
constmessages=[{role:"user",content:"What is the weather like in Paris today?"},];// Configure the request data.
// This includes the model ID to use, the messages list, and the tools list.
letdata={model:model,messages:messages,tools:tools,};// Generate the AIOS API's v1/chat/completions endpoint URL.
leturl=newURL("/v1/chat/completions",aios_base_url);// Send a POST request to the AIOS API.
// This request instructs the model to process the user's question and determine the necessary tool calls.
constresponse=awaitfetch(url,{method:"POST",headers:{"Content-Type":"application/json",},body:JSON.stringify(data),});// Parse the response body in JSON format.
letbody=awaitresponse.json();// Print the tool call information from the response generated by the AI model.
// This information indicates which tool the model should call.
console.log(JSON.stringify(body.choices[0].message.tool_calls));// Implementation of the weather function, always returns 14 degrees.
functiongetWeather(latitude,longitude){return"14℃";}// Extract tool call information from the first response.
// This retrieves the tool call information requested by the model.
consttoolCall=body.choices[0].message.tool_calls[0];// Parse the tool call arguments in JSON format.
// This extracts the parameters needed for the tool call.
constargs=JSON.parse(toolCall.function.arguments);// Call the actual function to get the result. (e.g., "14℃")
// At this step, the actual weather information lookup logic is executed.
constresult=getWeather(args.latitude,args.longitude);// Add the function call result as a **tool** message to the conversation context and call the model again,
// then the model generates an appropriate response using the function call result.
// Add the model's tool call message to messages to maintain the conversation context.
messages.push(body.choices[0].message);// Add the result of calling the actual function to messages.
// This allows the model to generate a final response based on the tool call result.
messages.push({role:"tool",tool_call_id:toolCall.id,content:String(result),});// Configure the second request data.
// This includes the model ID to use and the updated messages list.
// This request generates a final response based on the tool call result.
data={model:model,messages:messages,};// Send another POST request to the AIOS API.
// This request generates a final response based on the tool call result.
constresponse2=awaitfetch(url,{method:"POST",headers:{"Content-Type":"application/json",},body:JSON.stringify(data),});body=awaitresponse2.json();// Print the message generated by the AI in the second response.
// This is the final answer to the user's question.
console.log(JSON.stringify(body.choices[0].message));
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Define a function to get weather information.
// This function returns the current temperature in Celsius for the provided coordinates.
const tools = [
{
type:"function",
function: {
name:"get_weather",
description:"Get current temperature for provided coordinates in celsius.",
parameters: {
type:"object",
properties: {
latitude: { type:"number" },
longitude: { type:"number" },
},
required: ["latitude", "longitude"],
additionalProperties:false,
},
strict:true,
},
},
];
// Define the user message.
// The user is asking about today's weather in Paris.
const messages = [
{ role:"user", content:"What is the weather like in Paris today?" },
];
// Configure the request data.
// This includes the model ID to use, the messages list, and the tools list.
let data = {
model: model,
messages: messages,
tools: tools,
};
// Generate the AIOS API's v1/chat/completions endpoint URL.
let url =new URL("/v1/chat/completions", aios_base_url);
// Send a POST request to the AIOS API.
// This request instructs the model to process the user's question and determine the necessary tool calls.
const response =await fetch(url, {
method:"POST",
headers: {
"Content-Type":"application/json",
},
body: JSON.stringify(data),
});
// Parse the response body in JSON format.
let body =await response.json();
// Print the tool call information from the response generated by the AI model.
// This information indicates which tool the model should call.
console.log(JSON.stringify(body.choices[0].message.tool_calls));
// Implementation of the weather function, always returns 14 degrees.
function getWeather(latitude, longitude) {
return"14℃";
}
// Extract tool call information from the first response.
// This retrieves the tool call information requested by the model.
const toolCall = body.choices[0].message.tool_calls[0];
// Parse the tool call arguments in JSON format.
// This extracts the parameters needed for the tool call.
const args = JSON.parse(toolCall.function.arguments);
// Call the actual function to get the result. (e.g., "14℃")
// At this step, the actual weather information lookup logic is executed.
const result = getWeather(args.latitude, args.longitude);
// Add the function call result as a **tool** message to the conversation context and call the model again,
// then the model generates an appropriate response using the function call result.
// Add the model's tool call message to messages to maintain the conversation context.
messages.push(body.choices[0].message);
// Add the result of calling the actual function to messages.
// This allows the model to generate a final response based on the tool call result.
messages.push({
role:"tool",
tool_call_id: toolCall.id,
content: String(result),
});
// Configure the second request data.
// This includes the model ID to use and the updated messages list.
// This request generates a final response based on the tool call result.
data = {
model: model,
messages: messages,
};
// Send another POST request to the AIOS API.
// This request generates a final response based on the tool call result.
const response2 =await fetch(url, {
method:"POST",
headers: {
"Content-Type":"application/json",
},
body: JSON.stringify(data),
});
body =await response2.json();
// Print the message generated by the AI in the second response.
// This is the final answer to the user's question.
console.log(JSON.stringify(body.choices[0].message));
importOpenAIfrom"openai";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Define a function to get weather information.
// This function returns the current temperature in Celsius for the provided coordinates.
consttools=[{type:"function",function:{name:"get_weather",description:"Get current temperature for provided coordinates in celsius.",parameters:{type:"object",properties:{latitude:{type:"number"},longitude:{type:"number"},},required:["latitude","longitude"],additionalProperties:false,},strict:true,},},];// Define the user message.
// The user is asking about today's weather in Paris.
constmessages=[{role:"user",content:"What is the weather like in Paris today?"},];// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
constclient=newOpenAI({apiKey:"EMPTY_KEY",baseURL:newURL("v1",aios_base_url).href,});// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// The tools parameter provides metadata about the tools available to the model.
constresponse=awaitclient.chat.completions.create({model:model,messages:messages,tools:tools,});// Print the tool call information from the response generated by the AI model.
// This information indicates which tool the model should call.
console.log(JSON.stringify(response.choices[0].message.tool_calls));// Implementation of the weather function, always returns 14 degrees.
functiongetWeather(latitude,longitude){return"14℃";}// Extract tool call information from the first response.
// This retrieves the tool call information requested by the model.
consttoolCall=response.choices[0].message.tool_calls[0];// Parse the tool call arguments in JSON format.
// This extracts the parameters needed for the tool call.
constargs=JSON.parse(toolCall.function.arguments);// Call the actual function to get the result. (e.g., "14℃")
// At this step, the actual weather information lookup logic is executed.
constresult=getWeather(args.latitude,args.longitude);// Add the function call result as a **tool** message to the conversation context and call the model again,
// then the model generates an appropriate response using the function call result.
// Add the model's tool call message to messages to maintain the conversation context.
messages.push(response.choices[0].message);// Add the result of calling the actual function to messages.
// This allows the model to generate a final response based on the tool call result.
messages.push({role:"tool",tool_call_id:toolCall.id,content:String(result),});// Generate a second chat completion.
// This includes the model ID to use and the updated messages list.
// This request generates a final response based on the tool call result.
constresponse2=awaitclient.chat.completions.create({model:model,messages:messages,});// Print the message generated by the AI in the second response.
// This is the final answer to the user's question.
console.log(JSON.stringify(response2.choices[0].message));
import OpenAI from "openai";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Define a function to get weather information.
// This function returns the current temperature in Celsius for the provided coordinates.
const tools = [
{
type:"function",
function: {
name:"get_weather",
description:"Get current temperature for provided coordinates in celsius.",
parameters: {
type:"object",
properties: {
latitude: { type:"number" },
longitude: { type:"number" },
},
required: ["latitude", "longitude"],
additionalProperties:false,
},
strict:true,
},
},
];
// Define the user message.
// The user is asking about today's weather in Paris.
const messages = [
{ role:"user", content:"What is the weather like in Paris today?" },
];
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
const client =new OpenAI({
apiKey:"EMPTY_KEY",
baseURL:new URL("v1", aios_base_url).href,
});
// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// The tools parameter provides metadata about the tools available to the model.
const response =await client.chat.completions.create({
model: model,
messages: messages,
tools: tools,
});
// Print the tool call information from the response generated by the AI model.
// This information indicates which tool the model should call.
console.log(JSON.stringify(response.choices[0].message.tool_calls));
// Implementation of the weather function, always returns 14 degrees.
function getWeather(latitude, longitude) {
return"14℃";
}
// Extract tool call information from the first response.
// This retrieves the tool call information requested by the model.
const toolCall = response.choices[0].message.tool_calls[0];
// Parse the tool call arguments in JSON format.
// This extracts the parameters needed for the tool call.
const args = JSON.parse(toolCall.function.arguments);
// Call the actual function to get the result. (e.g., "14℃")
// At this step, the actual weather information lookup logic is executed.
const result = getWeather(args.latitude, args.longitude);
// Add the function call result as a **tool** message to the conversation context and call the model again,
// then the model generates an appropriate response using the function call result.
// Add the model's tool call message to messages to maintain the conversation context.
messages.push(response.choices[0].message);
// Add the result of calling the actual function to messages.
// This allows the model to generate a final response based on the tool call result.
messages.push({
role:"tool",
tool_call_id: toolCall.id,
content: String(result),
});
// Generate a second chat completion.
// This includes the model ID to use and the updated messages list.
// This request generates a final response based on the tool call result.
const response2 =await client.chat.completions.create({
model: model,
messages: messages,
});
// Print the message generated by the AI in the second response.
// This is the final answer to the user's question.
console.log(JSON.stringify(response2.choices[0].message));
import{HumanMessage}from"@langchain/core/messages";import{tool}from"@langchain/core/tools";import{ChatOpenAI}from"@langchain/openai";import{z}from"zod";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Define a tool function to get weather information.
// This function returns the current temperature in Celsius for the provided coordinates.
constgetWeather=tool(function(latitude,longitude){/**
* Get current temperature for provided coordinates in celsius.
*/return"14℃";},{name:"get_weather",description:"Get current temperature for provided coordinates in celsius.",schema:z.object({latitude:z.number(),longitude:z.number(),}),});// Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.
// base_url points to the v1 endpoint of the AIOS API,
// and api_key is the key required by AIOS, typically set to "EMPTY_KEY".
// The model parameter specifies the model ID to use.
constllm=newChatOpenAI({model:model,apiKey:"EMPTY_KEY",configuration:{baseURL:newURL("v1",aios_base_url).href,},});// Bind tools to the model.
// The getWeather function returns the current temperature in Celsius for the provided coordinates.
constllmWithTools=llm.bindTools([getWeather]);// Configure the list of chat messages.
// The user is asking about today's weather in Paris.
constmessages=[newHumanMessage("What is the weather like in Paris today?")];// Pass the list of messages to the chat LLM to get a response.
// The invoke method returns the model's output.
// This request instructs the model to process the user's question and determine the necessary tool calls.
constresponse=awaitllmWithTools.invoke(messages);// Print the tool call information from the response generated by the AI model.
// This information indicates which tool the model should call.
console.log(response.tool_calls);// Add the model's tool call message to messages to maintain the conversation context.
// This allows the model to remember and connect previous conversation content.
messages.push(response);// Call the actual tool function to get the result.
// At this step, the getWeather function is executed to return weather information.
consttoolCall=response.tool_calls[0];consttoolMessage=awaitgetWeather.invoke(toolCall);// Add the tool call result to messages.
// This allows the model to generate a final response based on the tool call result.
messages.push(toolMessage);// Perform a second request to get the final answer.
// Now the model generates an appropriate response to the user based on the tool call result.
constresponse2=awaitllm.invoke(messages);// Print the final AI model response.
console.log(response2.content);
import { HumanMessage } from "@langchain/core/messages";
import { tool } from "@langchain/core/tools";
import { ChatOpenAI } from "@langchain/openai";
import { z } from "zod";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Define a tool function to get weather information.
// This function returns the current temperature in Celsius for the provided coordinates.
const getWeather = tool(
function (latitude, longitude) {
/**
* Get current temperature for provided coordinates in celsius.
*/return"14℃";
},
{
name:"get_weather",
description:"Get current temperature for provided coordinates in celsius.",
schema: z.object({
latitude: z.number(),
longitude: z.number(),
}),
}
);
// Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.
// base_url points to the v1 endpoint of the AIOS API,
// and api_key is the key required by AIOS, typically set to "EMPTY_KEY".
// The model parameter specifies the model ID to use.
const llm =new ChatOpenAI({
model: model,
apiKey:"EMPTY_KEY",
configuration: {
baseURL:new URL("v1", aios_base_url).href,
},
});
// Bind tools to the model.
// The getWeather function returns the current temperature in Celsius for the provided coordinates.
const llmWithTools = llm.bindTools([getWeather]);
// Configure the list of chat messages.
// The user is asking about today's weather in Paris.
const messages = [new HumanMessage("What is the weather like in Paris today?")];
// Pass the list of messages to the chat LLM to get a response.
// The invoke method returns the model's output.
// This request instructs the model to process the user's question and determine the necessary tool calls.
const response =await llmWithTools.invoke(messages);
// Print the tool call information from the response generated by the AI model.
// This information indicates which tool the model should call.
console.log(response.tool_calls);
// Add the model's tool call message to messages to maintain the conversation context.
// This allows the model to remember and connect previous conversation content.
messages.push(response);
// Call the actual tool function to get the result.
// At this step, the getWeather function is executed to return weather information.
const toolCall = response.tool_calls[0];
const toolMessage =await getWeather.invoke(toolCall);
// Add the tool call result to messages.
// This allows the model to generate a final response based on the tool call result.
messages.push(toolMessage);
// Perform a second request to get the final answer.
// Now the model generates an appropriate response to the user based on the tool call result.
const response2 =await llm.invoke(messages);
// Print the final AI model response.
console.log(response2.content);
packagemainimport("bytes""encoding/json""fmt""io""net/http")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)// Define the Message structure.
// Role: Message role (user, assistant, tool, etc.)
// Content: Message content
// ToolCalls: Tool call information
// ToolCallId: Tool call identifier
typeMessagestruct{Rolestring`json:"role"`Contentstring`json:"content,omitempty"`ToolCalls[]map[string]any`json:"tool_calls,omitempty"`ToolCallIdstring`json:"tool_call_id,omitempty"`}// Define the POST request data structure.
// Model: Model ID to use
// Messages: List of messages
// Tools: List of available tools
// Stream: Whether to stream
typePostDatastruct{Modelstring`json:"model"`Messages[]Message`json:"messages"`Tools[]map[string]any`json:"tools,omitempty"`Streambool`json:"stream,omitempty"`}// Define a function to get weather information.
// This function always returns 14 degrees (sample implementation).
funcgetWeather(latitudefloat32,longitudefloat32)string{_=fmt.Sprintf("latitude: %f, longitude: %f",latitude,longitude)return"14℃"}funcmain(){// Define the user message.
// The user is asking about today's weather in Paris.
messages:=[]Message{{Role:"user",Content:"What is the weather like in Paris today?",},}// Define a function to get weather information.
// This tool returns the current temperature in Celsius for the provided coordinates.
tools:=[]map[string]any{{"type":"function","function":map[string]any{"name":"get_weather","description":"Get current temperature for provided coordinates in celsius.","parameters":map[string]any{"type":"object","properties":map[string]any{"latitude":map[string]string{"type":"number"},"longitude":map[string]string{"type":"number"},},"required":[]string{"latitude","longitude"},"additionalProperties":false,},"strict":true,},},}// Configure the request data.
// This includes the model ID to use, the messages list, and the tools list.
data:=PostData{Model:model,Messages:messages,Tools:tools,}// Serialize the request data to JSON format.
jsonData,err:=json.Marshal(data)iferr!=nil{panic(err)}// Send a POST request to the AIOS API's v1/chat/completions endpoint.
// This request instructs the model to process the user's question and determine the necessary tool calls.
response,err:=http.Post(aiosBaseUrl+"/v1/chat/completions","application/json",bytes.NewBuffer(jsonData))iferr!=nil{panic(err)}deferresponse.Body.Close()// Read the response body.
body,err:=io.ReadAll(response.Body)iferr!=nil{panic(err)}// Parse the response body to map format.
varvmap[string]interface{}json.Unmarshal(body,&v)// Extract message information from the first response.
choices:=v["choices"].([]interface{})choice:=choices[0].(map[string]interface{})message,err:=json.MarshalIndent(choice["message"],""," ")iferr!=nil{panic(err)}messageData:=choice["message"].(map[string]interface{})toolCalls:=messageData["tool_calls"].([]interface{})// Print the tool call information from the response generated by the AI model.
// This information indicates which tool the model should call.
toolCallJson,err:=json.MarshalIndent(toolCalls,""," ")iferr!=nil{panic(err)}fmt.Println(string(toolCallJson))// Extract tool call information from the first response.
// This retrieves the tool call information requested by the model.
toolCall:=toolCalls[0].(map[string]interface{})function:=toolCall["function"].(map[string]interface{})// Parse the tool call arguments from JSON string format to map format.
// This extracts the parameters needed for the tool call.
varargsmap[string]float32err=json.Unmarshal([]byte(function["arguments"].(string)),&args)iferr!=nil{panic(err)}// Call the actual function to get the result. (e.g., "14℃")
// At this step, the actual weather information lookup logic is executed.
result:=getWeather(args["latitude"],args["longitude"])// Convert the tool call result to a message.
vartoolMessageMessageerr=json.Unmarshal(message,&toolMessage)iferr!=nil{panic(err)}// Add the model's tool call message to messages to maintain the conversation context.
messages=append(messages,toolMessage)// Add the result of calling the actual function to messages.
// This allows the model to generate a final response based on the tool call result.
messages=append(messages,Message{Role:"tool",ToolCallId:toolCall["id"].(string),Content:string(result),})// Configure the second request data.
// This includes the model ID to use and the updated messages list.
// This request generates a final response based on the tool call result.
data=PostData{Model:model,Messages:messages,}jsonData,err=json.Marshal(data)iferr!=nil{panic(err)}// Send another POST request to the AIOS API.
// This request generates a final response based on the tool call result.
response2,err:=http.Post(aiosBaseUrl+"/v1/chat/completions","application/json",bytes.NewBuffer(jsonData))iferr!=nil{panic(err)}deferresponse2.Body.Close()// Read the second response body.
body,err=io.ReadAll(response2.Body)iferr!=nil{panic(err)}// Parse the second response to JSON format.
json.Unmarshal(body,&v)// Print the message generated by the AI in the second response.
// This is the final answer to the user's question.
choices=v["choices"].([]interface{})choice=choices[0].(map[string]interface{})message,err=json.MarshalIndent(choice["message"],""," ")iferr!=nil{panic(err)}fmt.Println(string(message))}
package main
import (
"bytes""encoding/json""fmt""io""net/http")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
// Define the Message structure.
// Role: Message role (user, assistant, tool, etc.)
// Content: Message content
// ToolCalls: Tool call information
// ToolCallId: Tool call identifier
type Message struct {
Role string`json:"role"` Content string`json:"content,omitempty"` ToolCalls []map[string]any `json:"tool_calls,omitempty"` ToolCallId string`json:"tool_call_id,omitempty"`}
// Define the POST request data structure.
// Model: Model ID to use
// Messages: List of messages
// Tools: List of available tools
// Stream: Whether to stream
type PostData struct {
Model string`json:"model"` Messages []Message `json:"messages"` Tools []map[string]any `json:"tools,omitempty"` Stream bool`json:"stream,omitempty"`}
// Define a function to get weather information.
// This function always returns 14 degrees (sample implementation).
funcgetWeather(latitude float32, longitude float32) string {
_ = fmt.Sprintf("latitude: %f, longitude: %f", latitude, longitude)
return"14℃"}
funcmain() {
// Define the user message.
// The user is asking about today's weather in Paris.
messages := []Message{
{
Role: "user",
Content: "What is the weather like in Paris today?",
},
}
// Define a function to get weather information.
// This tool returns the current temperature in Celsius for the provided coordinates.
tools := []map[string]any{
{
"type": "function",
"function": map[string]any{
"name": "get_weather",
"description": "Get current temperature for provided coordinates in celsius.",
"parameters": map[string]any{
"type": "object",
"properties": map[string]any{
"latitude": map[string]string{"type": "number"},
"longitude": map[string]string{"type": "number"},
},
"required": []string{"latitude", "longitude"},
"additionalProperties": false,
},
"strict": true,
},
},
}
// Configure the request data.
// This includes the model ID to use, the messages list, and the tools list.
data := PostData{
Model: model,
Messages: messages,
Tools: tools,
}
// Serialize the request data to JSON format.
jsonData, err := json.Marshal(data)
if err !=nil {
panic(err)
}
// Send a POST request to the AIOS API's v1/chat/completions endpoint.
// This request instructs the model to process the user's question and determine the necessary tool calls.
response, err := http.Post(aiosBaseUrl+"/v1/chat/completions", "application/json", bytes.NewBuffer(jsonData))
if err !=nil {
panic(err)
}
defer response.Body.Close()
// Read the response body.
body, err := io.ReadAll(response.Body)
if err !=nil {
panic(err)
}
// Parse the response body to map format.
var v map[string]interface{}
json.Unmarshal(body, &v)
// Extract message information from the first response.
choices := v["choices"].([]interface{})
choice := choices[0].(map[string]interface{})
message, err := json.MarshalIndent(choice["message"], "", " ")
if err !=nil {
panic(err)
}
messageData := choice["message"].(map[string]interface{})
toolCalls := messageData["tool_calls"].([]interface{})
// Print the tool call information from the response generated by the AI model.
// This information indicates which tool the model should call.
toolCallJson, err := json.MarshalIndent(toolCalls, "", " ")
if err !=nil {
panic(err)
}
fmt.Println(string(toolCallJson))
// Extract tool call information from the first response.
// This retrieves the tool call information requested by the model.
toolCall := toolCalls[0].(map[string]interface{})
function := toolCall["function"].(map[string]interface{})
// Parse the tool call arguments from JSON string format to map format.
// This extracts the parameters needed for the tool call.
var args map[string]float32 err = json.Unmarshal([]byte(function["arguments"].(string)), &args)
if err !=nil {
panic(err)
}
// Call the actual function to get the result. (e.g., "14℃")
// At this step, the actual weather information lookup logic is executed.
result :=getWeather(args["latitude"], args["longitude"])
// Convert the tool call result to a message.
var toolMessage Message
err = json.Unmarshal(message, &toolMessage)
if err !=nil {
panic(err)
}
// Add the model's tool call message to messages to maintain the conversation context.
messages = append(messages, toolMessage)
// Add the result of calling the actual function to messages.
// This allows the model to generate a final response based on the tool call result.
messages = append(messages, Message{
Role: "tool",
ToolCallId: toolCall["id"].(string),
Content: string(result),
})
// Configure the second request data.
// This includes the model ID to use and the updated messages list.
// This request generates a final response based on the tool call result.
data = PostData{
Model: model,
Messages: messages,
}
jsonData, err = json.Marshal(data)
if err !=nil {
panic(err)
}
// Send another POST request to the AIOS API.
// This request generates a final response based on the tool call result.
response2, err := http.Post(aiosBaseUrl+"/v1/chat/completions", "application/json", bytes.NewBuffer(jsonData))
if err !=nil {
panic(err)
}
defer response2.Body.Close()
// Read the second response body.
body, err = io.ReadAll(response2.Body)
if err !=nil {
panic(err)
}
// Parse the second response to JSON format.
json.Unmarshal(body, &v)
// Print the message generated by the AI in the second response.
// This is the final answer to the user's question.
choices = v["choices"].([]interface{})
choice = choices[0].(map[string]interface{})
message, err = json.MarshalIndent(choice["message"], "", " ")
if err !=nil {
panic(err)
}
fmt.Println(string(message))
}
packagemainimport("context""encoding/json""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)// Define a function to get weather information.
// This function always returns 14 degrees (sample implementation).
funcgetWeather(latitudefloat32,longitudefloat32)string{_=fmt.Sprintf("latitude: %f, longitude: %f",latitude,longitude)return"14℃"}funcmain(){// Create an OpenAI client.
// base_url points to the v1 endpoint of the AIOS API.
client:=openai.NewClient(option.WithBaseURL(aiosBaseUrl+"/v1"),)// Define the user message.
// The user is asking about today's weather in Paris.
messages:=[]openai.ChatCompletionMessageParamUnion{openai.UserMessage("What is the weather like in Paris today?"),}// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// The tools parameter provides metadata about the tools available to the model.
response,err:=client.Chat.Completions.New(context.TODO(),openai.ChatCompletionNewParams{Model:model,Messages:messages,Tools:[]openai.ChatCompletionToolParam{{Function:openai.FunctionDefinitionParam{Name:"get_weather",Description:openai.String("Get current temperature for provided coordinates in celsius."),Parameters:openai.FunctionParameters{"type":"object","properties":map[string]interface{}{"latitude":map[string]string{"type":"number",},"longitude":map[string]string{"type":"number",},},"required":[]string{"latitude","longitude"},"additionalProperties":false,},Strict:openai.Bool(true),},},},})iferr!=nil{panic(err)}// Print the response generated by the AI model.
// This response includes tool call information.
fmt.Println([]string{response.Choices[0].Message.ToolCalls[0].RawJSON()})// Extract tool call information from the first response.
// This retrieves the tool call information requested by the model.
varvmap[string]float32toolCall:=response.Choices[0].Message.ToolCalls[0]args:=toolCall.Function.Arguments// Parse the tool call arguments from JSON string format to map format.
// This extracts the parameters needed for the tool call.
err=json.Unmarshal([]byte(args),&v)iferr!=nil{panic(err)}// Call the actual function to get the result. (e.g., "14℃")
// At this step, the actual weather information lookup logic is executed.
result:=getWeather(v["latitude"],v["longitude"])// Add the function call result as a **tool** message to the conversation context and call the model again,
// then the model generates an appropriate response using the function call result.
// Add the model's tool call message to messages to maintain the conversation context.
messages=append(messages,response.Choices[0].Message.ToParam())// Add the result of calling the actual function to messages.
// This allows the model to generate a final response based on the tool call result.
messages=append(messages,openai.ToolMessage(string(result),toolCall.ID))// Generate a second chat completion.
// This includes the model ID to use and the updated messages list.
// This request generates a final response based on the tool call result.
response2,err:=client.Chat.Completions.New(context.TODO(),openai.ChatCompletionNewParams{Model:model,Messages:messages,})iferr!=nil{panic(err)}// Print the message generated by the AI in the second response.
// This is the final answer to the user's question.
fmt.Println(response2.Choices[0].Message.RawJSON())}
package main
import (
"context""encoding/json""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
// Define a function to get weather information.
// This function always returns 14 degrees (sample implementation).
funcgetWeather(latitude float32, longitude float32) string {
_ = fmt.Sprintf("latitude: %f, longitude: %f", latitude, longitude)
return"14℃"}
funcmain() {
// Create an OpenAI client.
// base_url points to the v1 endpoint of the AIOS API.
client := openai.NewClient(
option.WithBaseURL(aiosBaseUrl +"/v1"),
)
// Define the user message.
// The user is asking about today's weather in Paris.
messages := []openai.ChatCompletionMessageParamUnion{
openai.UserMessage("What is the weather like in Paris today?"),
}
// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// The tools parameter provides metadata about the tools available to the model.
response, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
Model: model,
Messages: messages,
Tools: []openai.ChatCompletionToolParam{
{
Function: openai.FunctionDefinitionParam{
Name: "get_weather",
Description: openai.String("Get current temperature for provided coordinates in celsius."),
Parameters: openai.FunctionParameters{
"type": "object",
"properties": map[string]interface{}{
"latitude": map[string]string{
"type": "number",
},
"longitude": map[string]string{
"type": "number",
},
},
"required": []string{"latitude", "longitude"},
"additionalProperties": false,
},
Strict: openai.Bool(true),
},
},
},
})
if err !=nil {
panic(err)
}
// Print the response generated by the AI model.
// This response includes tool call information.
fmt.Println([]string{response.Choices[0].Message.ToolCalls[0].RawJSON()})
// Extract tool call information from the first response.
// This retrieves the tool call information requested by the model.
var v map[string]float32 toolCall := response.Choices[0].Message.ToolCalls[0]
args := toolCall.Function.Arguments
// Parse the tool call arguments from JSON string format to map format.
// This extracts the parameters needed for the tool call.
err = json.Unmarshal([]byte(args), &v)
if err !=nil {
panic(err)
}
// Call the actual function to get the result. (e.g., "14℃")
// At this step, the actual weather information lookup logic is executed.
result :=getWeather(v["latitude"], v["longitude"])
// Add the function call result as a **tool** message to the conversation context and call the model again,
// then the model generates an appropriate response using the function call result.
// Add the model's tool call message to messages to maintain the conversation context.
messages = append(messages, response.Choices[0].Message.ToParam())
// Add the result of calling the actual function to messages.
// This allows the model to generate a final response based on the tool call result.
messages = append(messages, openai.ToolMessage(string(result), toolCall.ID))
// Generate a second chat completion.
// This includes the model ID to use and the updated messages list.
// This request generates a final response based on the tool call result.
response2, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
Model: model,
Messages: messages,
})
if err !=nil {
panic(err)
}
// Print the message generated by the AI in the second response.
// This is the final answer to the user's question.
fmt.Println(response2.Choices[0].Message.RawJSON())
}
Code block. tool call request
Response
In the first response, you can check the execution method of the tool that the model determined to use in message.tool_calls of choices.
In function of tool_calls, you can check that the get_weather function is used and what arguments are passed to execute it.
The second request included three messages in the messages:
The initial user message
The tool calling message generated by the first model
The tool message containing the result of executing the get_weather tool
In the second response, the model generates a final response using all the content of the above messages.
{'content':'The current weather in Paris is 14℃.','refusal':None,'role':'assistant','annotations':None,'audio':None,'function_call':None,'tool_calls':[],'reasoning_content':'We have user asking weather in Paris today. We called ''get_weather function with coordinates and got "14℃" as ''comment. We need to respond. Should incorporate info ''and maybe note we are using approximate. Provide ''answer.',}
reasoning
Request
For models that support reasoning, you can check the reasoning value as follows.
Warning
Reasoning support models generate many tokens during the inference process, so the answer generation time may take significantly longer.
Color mode
importjsonimportrequestsfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# In this example, the user is asked to compare which of two numbers is greater.# "Think step by step" is a prompt that encourages the model to think through logical steps.data={"model":model,"messages":[{"role":"user","content":"Think step by step. 9.11 and 9.8, which is greater?"}]}# Send a POST request to the AIOS API's v1/chat/completions endpoint.# This request instructs the model to process the user's question.response=requests.post(urljoin(aios_base_url,"v1/chat/completions"),json=data)body=json.loads(response.text)# Print the response generated by the AI model.print(body["choices"][0]["message"])
importjsonimportrequestsfromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# In this example, the user is asked to compare which of two numbers is greater.# "Think step by step" is a prompt that encourages the model to think through logical steps.data = {
"model": model,
"messages": [
{"role": "user", "content": "Think step by step. 9.11 and 9.8, which is greater?"}
]
}
# Send a POST request to the AIOS API's v1/chat/completions endpoint.# This request instructs the model to process the user's question.response = requests.post(urljoin(aios_base_url, "v1/chat/completions"), json=data)
body = json.loads(response.text)
# Print the response generated by the AI model.print(body["choices"][0]["message"])
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")# Generate a chat completion using the AIOS model.# The model parameter specifies the model ID to use.# The messages parameter is a list of messages containing the user message.# "Think step by step" is a prompt that encourages the model to think through logical steps.response=client.chat.completions.create(model=model,messages=[{"role":"user","content":"Think step by step. 9.11 and 9.8, which is greater?"}],)# Print the response generated by the AI model.print(response.choices[0].message.model_dump())
fromopenaiimport OpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".client = OpenAI(base_url=urljoin(aios_base_url, "v1"), api_key="EMPTY_KEY")
# Generate a chat completion using the AIOS model.# The model parameter specifies the model ID to use.# The messages parameter is a list of messages containing the user message.# "Think step by step" is a prompt that encourages the model to think through logical steps.response = client.chat.completions.create(
model=model,
messages=[
{"role": "user", "content": "Think step by step. 9.11 and 9.8, which is greater?"}
],
)
# Print the response generated by the AI model.print(response.choices[0].message.model_dump())
fromlangchain_openaiimportChatOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".# The model parameter specifies the model ID to use.chat_llm=ChatOpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY",model=model)# Configure the list of chat messages.# The user is asking to compare which of two numbers is greater.# "Think step by step" is a prompt that encourages the model to think through logical steps.messages=[("human","Think step by step. 9.11 and 9.8, which is greater?"),]# Pass the list of messages to the chat LLM to get a response.# The invoke method returns the model's output.# This request instructs the model to process the user's question.chat_completion=chat_llm.invoke(messages)# Print the response generated by the AI model.print(chat_completion.model_dump())
fromlangchain_openaiimport ChatOpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".# The model parameter specifies the model ID to use.chat_llm = ChatOpenAI(
base_url=urljoin(aios_base_url, "v1"),
api_key="EMPTY_KEY",
model=model
)
# Configure the list of chat messages.# The user is asking to compare which of two numbers is greater.# "Think step by step" is a prompt that encourages the model to think through logical steps.messages = [
("human", "Think step by step. 9.11 and 9.8, which is greater?"),
]
# Pass the list of messages to the chat LLM to get a response.# The invoke method returns the model's output.# This request instructs the model to process the user's question.chat_completion = chat_llm.invoke(messages)
# Print the response generated by the AI model.print(chat_completion.model_dump())
constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Configure the request data.
// In this example, the user is asked to compare which of two numbers is greater.
// "Think step by step" is a prompt that encourages the model to think through logical steps.
constdata={model:model,messages:[{role:"user",content:"Think step by step. 9.11 and 9.8, which is greater?",},],};// Generate the AIOS API's v1/chat/completions endpoint URL.
leturl=newURL("/v1/chat/completions",aios_base_url);// Send a POST request to the AIOS API.
// This request instructs the model to process the user's question.
constresponse=awaitfetch(url,{method:"POST",headers:{"Content-Type":"application/json",},body:JSON.stringify(data),});constbody=awaitresponse.json();// Print the response generated by the AI model.
console.log(body.choices[0].message);
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Configure the request data.
// In this example, the user is asked to compare which of two numbers is greater.
// "Think step by step" is a prompt that encourages the model to think through logical steps.
const data = {
model: model,
messages: [
{
role:"user",
content:"Think step by step. 9.11 and 9.8, which is greater?",
},
],
};
// Generate the AIOS API's v1/chat/completions endpoint URL.
let url =new URL("/v1/chat/completions", aios_base_url);
// Send a POST request to the AIOS API.
// This request instructs the model to process the user's question.
const response =await fetch(url, {
method:"POST",
headers: {
"Content-Type":"application/json",
},
body: JSON.stringify(data),
});
const body =await response.json();
// Print the response generated by the AI model.
console.log(body.choices[0].message);
importOpenAIfrom"openai";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
constclient=newOpenAI({apiKey:"EMPTY_KEY",baseURL:newURL("v1",aios_base_url).href,});// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// "Think step by step" is a prompt that encourages the model to think through logical steps.
constresponse=awaitclient.chat.completions.create({model:model,messages:[{role:"user",content:"Think step by step. 9.11 and 9.8, which is greater?",},],});// Print the response generated by the AI model.
console.log(response.choices[0].message);
import OpenAI from "openai";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
const client =new OpenAI({
apiKey:"EMPTY_KEY",
baseURL:new URL("v1", aios_base_url).href,
});
// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// "Think step by step" is a prompt that encourages the model to think through logical steps.
const response =await client.chat.completions.create({
model: model,
messages: [
{
role:"user",
content:"Think step by step. 9.11 and 9.8, which is greater?",
},
],
});
// Print the response generated by the AI model.
console.log(response.choices[0].message);
packagemainimport("bytes""encoding/json""fmt""io""net/http")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)// Define the Message structure.
// Role: Message role (user, assistant, etc.)
// Content: Message content
typeMessagestruct{Rolestring`json:"role"`Contentstring`json:"content"`}// Define the POST request data structure.
// Model: Model ID to use
// Messages: List of messages
// Stream: Whether to stream
typePostDatastruct{Modelstring`json:"model"`Messages[]Message`json:"messages"`Streambool`json:"stream,omitempty"`}funcmain(){// Configure the request data.
// In this example, the user is asked to compare which of two numbers is greater.
// "Think step by step" is a prompt that encourages the model to think through logical steps.
data:=PostData{Model:model,Messages:[]Message{{Role:"user",Content:"Think step by step. 9.11 and 9.8, which is greater?",},},}// Serialize the request data to JSON format.
jsonData,err:=json.Marshal(data)iferr!=nil{panic(err)}// Send a POST request to the AIOS API's v1/chat/completions endpoint.
response,err:=http.Post(aiosBaseUrl+"/v1/chat/completions","application/json",bytes.NewBuffer(jsonData))iferr!=nil{panic(err)}deferresponse.Body.Close()// Read the response body.
body,err:=io.ReadAll(response.Body)iferr!=nil{panic(err)}// Parse the response body to JSON format.
// This converts the model's response received from the server into structured data.
varvmap[string]interface{}json.Unmarshal(body,&v)choices:=v["choices"].([]interface{})choice:=choices[0].(map[string]interface{})message,err:=json.MarshalIndent(choice["message"],""," ")iferr!=nil{panic(err)}// Print the response generated by the AI model.
fmt.Println(string(message))}
package main
import (
"bytes""encoding/json""fmt""io""net/http")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
// Define the Message structure.
// Role: Message role (user, assistant, etc.)
// Content: Message content
type Message struct {
Role string`json:"role"` Content string`json:"content"`}
// Define the POST request data structure.
// Model: Model ID to use
// Messages: List of messages
// Stream: Whether to stream
type PostData struct {
Model string`json:"model"` Messages []Message `json:"messages"` Stream bool`json:"stream,omitempty"`}
funcmain() {
// Configure the request data.
// In this example, the user is asked to compare which of two numbers is greater.
// "Think step by step" is a prompt that encourages the model to think through logical steps.
data := PostData{
Model: model,
Messages: []Message{
{
Role: "user",
Content: "Think step by step. 9.11 and 9.8, which is greater?",
},
},
}
// Serialize the request data to JSON format.
jsonData, err := json.Marshal(data)
if err !=nil {
panic(err)
}
// Send a POST request to the AIOS API's v1/chat/completions endpoint.
response, err := http.Post(aiosBaseUrl+"/v1/chat/completions", "application/json", bytes.NewBuffer(jsonData))
if err !=nil {
panic(err)
}
defer response.Body.Close()
// Read the response body.
body, err := io.ReadAll(response.Body)
if err !=nil {
panic(err)
}
// Parse the response body to JSON format.
// This converts the model's response received from the server into structured data.
var v map[string]interface{}
json.Unmarshal(body, &v)
choices := v["choices"].([]interface{})
choice := choices[0].(map[string]interface{})
message, err := json.MarshalIndent(choice["message"], "", " ")
if err !=nil {
panic(err)
}
// Print the response generated by the AI model.
fmt.Println(string(message))
}
packagemainimport("context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)funcmain(){// Create an OpenAI client.
// base_url points to the v1 endpoint of the AIOS API.
client:=openai.NewClient(option.WithBaseURL(aiosBaseUrl+"/v1"),)// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// "Think step by step" is a prompt that encourages the model to think through logical steps.
response,err:=client.Chat.Completions.New(context.TODO(),openai.ChatCompletionNewParams{Model:model,Messages:[]openai.ChatCompletionMessageParamUnion{openai.UserMessage("Think step by step. 9.11 and 9.8, which is greater?"),},})iferr!=nil{panic(err)}// Print the response generated by the AI model.
fmt.Println(response.Choices[0].Message.RawJSON())}
package main
import (
"context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
funcmain() {
// Create an OpenAI client.
// base_url points to the v1 endpoint of the AIOS API.
client := openai.NewClient(
option.WithBaseURL(aiosBaseUrl +"/v1"),
)
// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// "Think step by step" is a prompt that encourages the model to think through logical steps.
response, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
Model: model,
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage("Think step by step. 9.11 and 9.8, which is greater?"),
},
})
if err !=nil {
panic(err)
}
// Print the response generated by the AI model.
fmt.Println(response.Choices[0].Message.RawJSON())
}
Code block. reasoning request
Response
If you check the message field of choices, you can see reasoning_content in addition to content.
reasoning_content refers to the tokens generated during the reasoning phase before generating the final answer.
{'annotations':None,'audio':None,'content':'Sure! Let’s compare the two numbers step by step.\n''\n''1. **Identify the numbers** \n'' - First number: **9.11** \n'' - Second number: **9.8**\n''\n''2. **Look at the whole-number part** \n'' Both numbers have the same whole-number part, **9**. So the ''comparison will depend on the decimal part.\n''\n''3. **Compare the decimal parts** \n'' - Decimal part of 9.11 = **0.11** \n'' - Decimal part of 9.8 = **0.80** (since 9.8 = 9.80)\n''\n''4. **Determine which decimal part is larger** \n'' - 0.80 is greater than 0.11.\n''\n''5. **Conclude** \n'' Because the whole-number parts are equal and the decimal part ''of 9.8 is larger, **9.8 is greater than 9.11**.','function_call':None,'reasoning_content':'User asks: "Think step by step. 9.11 and 9.8, which is ''greater?" We need to compare numbers 9.11 and 9.8. ''Value: 9.11 < 9.8, so 9.8 is greater. Provide ''step-by-step reasoning. No policy conflict.','refusal':None,'role':'assistant','tool_calls':[]}
image to text
For models that support vision, you can input images as follows.
Fig. Input image
Warning
Input images for vision support models have size and quantity limits.
For information about image input limits, please refer to Provided Models.
Request
You can input images in base64-encoded data URL format with MIME type.
Color mode
importbase64importjsonimportrequestsfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.image_path="image/path.jpg"# Define a function to Base64 encode an image.# This converts the image to text format so it can be transmitted to the API.defencode_image(image_path:str):withopen(image_path,"rb")asimage_file:returnbase64.b64encode(image_file.read()).decode("utf-8")# Encode the image in Base64 format.base64_image=encode_image(image_path)# Configure the request data.# In this example, the user is asked to ask a question about the image.# The image is transmitted as a Base64-encoded string.data={"model":model,"messages":[{"role":"user","content":[{"type":"text","text":"what's in this image?"},{"type":"image_url","image_url":{"url":f"data:image/jpeg;base64,{base64_image}",},},]},]}# Send a POST request to the AIOS API's v1/chat/completions endpoint.# This request asks the model to analyze the image.response=requests.post(urljoin(aios_base_url,"v1/chat/completions"),json=data)body=json.loads(response.text)# Print the response generated by the AI model.# This response is the model's description of the image content.print(body["choices"][0]["message"])
importbase64importjsonimportrequestsfromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.image_path ="image/path.jpg"# Define a function to Base64 encode an image.# This converts the image to text format so it can be transmitted to the API.defencode_image(image_path: str):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
# Encode the image in Base64 format.base64_image = encode_image(image_path)
# Configure the request data.# In this example, the user is asked to ask a question about the image.# The image is transmitted as a Base64-encoded string.data = {
"model": model,
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "what's in this image?"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
},
},
]
},
]
}
# Send a POST request to the AIOS API's v1/chat/completions endpoint.# This request asks the model to analyze the image.response = requests.post(urljoin(aios_base_url, "v1/chat/completions"), json=data)
body = json.loads(response.text)
# Print the response generated by the AI model.# This response is the model's description of the image content.print(body["choices"][0]["message"])
importbase64fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")image_path="image/path.jpg"# Define a function to Base64 encode an image.# This converts the image to text format so it can be transmitted to the API.defencode_image(image_path:str):withopen(image_path,"rb")asimage_file:returnbase64.b64encode(image_file.read()).decode("utf-8")# Encode the image in Base64 format.base64_image=encode_image(image_path)# Generate a chat completion using the AIOS model.# The model parameter specifies the model ID to use.# The messages parameter is a list of messages containing the user message.# In this example, the user is asked to ask a question about the image.# The image is transmitted as a Base64-encoded string.response=client.chat.completions.create(model=model,messages=[{"role":"user","content":[{"type":"text","text":"what's in this image?"},{"type":"image_url","image_url":{"url":f"data:image/jpeg;base64,{base64_image}",},},]},],)# Print the response generated by the AI model.# This response is the model's description of the image content.print(response.choices[0].message.model_dump())
importbase64fromopenaiimport OpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".client = OpenAI(base_url=urljoin(aios_base_url, "v1"), api_key="EMPTY_KEY")
image_path ="image/path.jpg"# Define a function to Base64 encode an image.# This converts the image to text format so it can be transmitted to the API.defencode_image(image_path: str):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
# Encode the image in Base64 format.base64_image = encode_image(image_path)
# Generate a chat completion using the AIOS model.# The model parameter specifies the model ID to use.# The messages parameter is a list of messages containing the user message.# In this example, the user is asked to ask a question about the image.# The image is transmitted as a Base64-encoded string.response = client.chat.completions.create(
model=model,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "what's in this image?"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
},
},
]
},
],
)
# Print the response generated by the AI model.# This response is the model's description of the image content.print(response.choices[0].message.model_dump())
importbase64fromlangchain_openaiimportChatOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".# The model parameter specifies the model ID to use.chat_llm=ChatOpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY",model=model)image_path="image/path.jpg"# Define a function to Base64 encode an image.# This converts the image to text format so it can be transmitted to the API.defencode_image(image_path:str):withopen(image_path,"rb")asimage_file:returnbase64.b64encode(image_file.read()).decode("utf-8")# Encode the image in Base64 format.base64_image=encode_image(image_path)# Configure the list of chat messages.# In this example, the user is asked to ask a question about the image.# The image is transmitted as a Base64-encoded string.messages=[{"role":"user","content":[{"type":"text","text":"what's in this image?"},{"type":"image_url","image_url":{"url":f"data:image/jpeg;base64,{base64_image}",},},]},]# Pass the list of messages to the chat LLM to get a response.# The invoke method returns the model's output.# This request asks the model to analyze the image.chat_completion=chat_llm.invoke(messages)# Print the response generated by the AI model.# This response is the model's description of the image content.print(chat_completion.model_dump())
importbase64fromlangchain_openaiimport ChatOpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.# base_url points to the v1 endpoint of the AIOS API,# and api_key is the key required by AIOS, typically set to "EMPTY_KEY".# The model parameter specifies the model ID to use.chat_llm = ChatOpenAI(
base_url=urljoin(aios_base_url, "v1"),
api_key="EMPTY_KEY",
model=model
)
image_path ="image/path.jpg"# Define a function to Base64 encode an image.# This converts the image to text format so it can be transmitted to the API.defencode_image(image_path: str):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
# Encode the image in Base64 format.base64_image = encode_image(image_path)
# Configure the list of chat messages.# In this example, the user is asked to ask a question about the image.# The image is transmitted as a Base64-encoded string.messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "what's in this image?"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
},
},
]
},
]
# Pass the list of messages to the chat LLM to get a response.# The invoke method returns the model's output.# This request asks the model to analyze the image.chat_completion = chat_llm.invoke(messages)
# Print the response generated by the AI model.# This response is the model's description of the image content.print(chat_completion.model_dump())
import{readFile}from"fs/promises";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
constimagePath="image/path.jpg";// Define a function to convert an image file to Base64.
// This converts the image to text format so it can be transmitted to the API.
asyncfunctionimageFileToBase64(imagePath){// Read file contents as buffer
constfileBuffer=awaitreadFile(imagePath);// Convert buffer to Base64 string
returnfileBuffer.toString("base64");}// Convert the image file to Base64 format.
constbase64Image=awaitimageFileToBase64(imagePath);// Configure the request data.
// In this example, the user is asked to ask a question about the image.
// The image is transmitted as a Base64-encoded string.
constdata={model:model,messages:[{role:"user",content:[{type:"text",text:"what's in this image?"},{type:"image_url",image_url:{url:`data:image/jpeg;base64,${base64Image}`,},},],},],};// Generate the AIOS API's v1/chat/completions endpoint URL.
leturl=newURL("/v1/chat/completions",aios_base_url);// Send a POST request to the AIOS API.
// This request asks the model to analyze the image.
constresponse=awaitfetch(url,{method:"POST",headers:{"Content-Type":"application/json",},body:JSON.stringify(data),});constbody=awaitresponse.json();// Print the response generated by the AI model.
// This response is the model's description of the image content.
console.log(body.choices[0].message);
import { readFile } from "fs/promises";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
const imagePath ="image/path.jpg";
// Define a function to convert an image file to Base64.
// This converts the image to text format so it can be transmitted to the API.
asyncfunction imageFileToBase64(imagePath) {
// Read file contents as buffer
const fileBuffer =await readFile(imagePath);
// Convert buffer to Base64 string
return fileBuffer.toString("base64");
}
// Convert the image file to Base64 format.
const base64Image =await imageFileToBase64(imagePath);
// Configure the request data.
// In this example, the user is asked to ask a question about the image.
// The image is transmitted as a Base64-encoded string.
const data = {
model: model,
messages: [
{
role:"user",
content: [
{ type:"text", text:"what's in this image?" },
{
type:"image_url",
image_url: {
url:`data:image/jpeg;base64,${base64Image}`,
},
},
],
},
],
};
// Generate the AIOS API's v1/chat/completions endpoint URL.
let url =new URL("/v1/chat/completions", aios_base_url);
// Send a POST request to the AIOS API.
// This request asks the model to analyze the image.
const response =await fetch(url, {
method:"POST",
headers: {
"Content-Type":"application/json",
},
body: JSON.stringify(data),
});
const body =await response.json();
// Print the response generated by the AI model.
// This response is the model's description of the image content.
console.log(body.choices[0].message);
importOpenAIfrom"openai";import{readFile}from"fs/promises";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
constimagePath="image/path.jpg";// Define a function to convert an image file to Base64.
// This converts the image to text format so it can be transmitted to the API.
asyncfunctionimageFileToBase64(imagePath){// Read file contents as buffer
constfileBuffer=awaitreadFile(imagePath);// Convert buffer to Base64 string
returnfileBuffer.toString("base64");}// Convert the image file to Base64 format.
constbase64Image=awaitimageFileToBase64(imagePath);// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
constclient=newOpenAI({apiKey:"EMPTY_KEY",baseURL:newURL("v1",aios_base_url).href,});// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// In this example, the user is asked to ask a question about the image.
// The image is transmitted as a Base64-encoded string.
constresponse=awaitclient.chat.completions.create({model:model,messages:[{role:"user",content:[{type:"text",text:"what's in this image?"},{type:"image_url",image_url:{url:`data:image/jpeg;base64,${base64Image}`,},},],},],});// Print the response generated by the AI model.
// This response is the model's description of the image content.
console.log(response.choices[0].message);
import OpenAI from "openai";
import { readFile } from "fs/promises";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
const imagePath ="image/path.jpg";
// Define a function to convert an image file to Base64.
// This converts the image to text format so it can be transmitted to the API.
asyncfunction imageFileToBase64(imagePath) {
// Read file contents as buffer
const fileBuffer =await readFile(imagePath);
// Convert buffer to Base64 string
return fileBuffer.toString("base64");
}
// Convert the image file to Base64 format.
const base64Image =await imageFileToBase64(imagePath);
// Create an OpenAI client.
// apiKey is the key required by AIOS, typically set to "EMPTY_KEY".
// baseURL points to the v1 endpoint of the AIOS API.
const client =new OpenAI({
apiKey:"EMPTY_KEY",
baseURL:new URL("v1", aios_base_url).href,
});
// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// In this example, the user is asked to ask a question about the image.
// The image is transmitted as a Base64-encoded string.
const response =await client.chat.completions.create({
model: model,
messages: [
{
role:"user",
content: [
{ type:"text", text:"what's in this image?" },
{
type:"image_url",
image_url: {
url:`data:image/jpeg;base64,${base64Image}`,
},
},
],
},
],
});
// Print the response generated by the AI model.
// This response is the model's description of the image content.
console.log(response.choices[0].message);
import{HumanMessage}from"@langchain/core/messages";import{ChatOpenAI}from"@langchain/openai";import{readFile}from"fs/promises";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
constimagePath="image/path.jpg";// Define a function to convert an image file to Base64.
// This converts the image to text format so it can be transmitted to the API.
asyncfunctionimageFileToBase64(imagePath){// Read file contents as buffer
constfileBuffer=awaitreadFile(imagePath);// Convert buffer to Base64 string
returnfileBuffer.toString("base64");}// Convert the image file to Base64 format.
constbase64Image=awaitimageFileToBase64(imagePath);// Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.
// base_url points to the v1 endpoint of the AIOS API,
// and api_key is the key required by AIOS, typically set to "EMPTY_KEY".
// The model parameter specifies the model ID to use.
constllm=newChatOpenAI({model:model,apiKey:"EMPTY_KEY",configuration:{baseURL:newURL("v1",aios_base_url).href,},});// Configure the list of chat messages.
// In this example, the user is asked to ask a question about the image.
// The image is transmitted as a Base64-encoded string.
constmessages=[newHumanMessage({content:[{type:"text",text:"what's in this image?"},{type:"image_url",image_url:{url:`data:image/jpeg;base64,${base64Image}`,},},],}),];// Pass the list of messages to the chat LLM to get a response.
// The invoke method returns the model's output.
// This request asks the model to analyze the image.
constresponse=awaitllm.invoke(messages);// Print the response generated by the AI model.
// This response is the model's description of the image content.
console.log(response.content);
import { HumanMessage } from "@langchain/core/messages";
import { ChatOpenAI } from "@langchain/openai";
import { readFile } from "fs/promises";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
const imagePath ="image/path.jpg";
// Define a function to convert an image file to Base64.
// This converts the image to text format so it can be transmitted to the API.
asyncfunction imageFileToBase64(imagePath) {
// Read file contents as buffer
const fileBuffer =await readFile(imagePath);
// Convert buffer to Base64 string
return fileBuffer.toString("base64");
}
// Convert the image file to Base64 format.
const base64Image =await imageFileToBase64(imagePath);
// Create a chat LLM (Large Language Model) instance using LangChain's ChatOpenAI class.
// base_url points to the v1 endpoint of the AIOS API,
// and api_key is the key required by AIOS, typically set to "EMPTY_KEY".
// The model parameter specifies the model ID to use.
const llm =new ChatOpenAI({
model: model,
apiKey:"EMPTY_KEY",
configuration: {
baseURL:new URL("v1", aios_base_url).href,
},
});
// Configure the list of chat messages.
// In this example, the user is asked to ask a question about the image.
// The image is transmitted as a Base64-encoded string.
const messages = [
new HumanMessage({
content: [
{ type:"text", text:"what's in this image?" },
{
type:"image_url",
image_url: {
url:`data:image/jpeg;base64,${base64Image}`,
},
},
],
}),
];
// Pass the list of messages to the chat LLM to get a response.
// The invoke method returns the model's output.
// This request asks the model to analyze the image.
const response =await llm.invoke(messages);
// Print the response generated by the AI model.
// This response is the model's description of the image content.
console.log(response.content);
packagemainimport("bytes""encoding/base64""encoding/json""fmt""io""net/http""os")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)varimagePath="image/path.jpg"// Define the Message structure.
// Role: Message role (user, assistant, etc.)
// Content: Message content (including text and image URL)
typeMessagestruct{Rolestring`json:"role"`Content[]map[string]interface{}`json:"content"`}// Define the POST request data structure.
// Model: Model ID to use
// Messages: List of messages
// Stream: Whether to stream
typePostDatastruct{Modelstring`json:"model"`Messages[]Message`json:"messages"`Streambool`json:"stream,omitempty"`}// Define a function to Base64 encode an image file.
// This converts the image to text format so it can be transmitted to the API.
funcimageFileToBase64(imagePathstring)(string,error){data,err:=os.ReadFile(imagePath)iferr!=nil{return"",err}returnbase64.StdEncoding.EncodeToString([]byte(data)),nil}funcmain(){// Encode the image file in Base64 format.
base64Image,err:=imageFileToBase64(imagePath)iferr!=nil{panic(err)}// Configure the request data.
// In this example, the user is asked to ask a question about the image.
// The image is transmitted as a Base64-encoded string.
data:=PostData{Model:model,Messages:[]Message{{Role:"user",Content:[]map[string]interface{}{{"type":"text","text":"what's in this image?",},{"type":"image_url","image_url":map[string]string{"url":fmt.Sprintf("data:image/jpeg;base64,%s",base64Image),},},},},},}// Serialize the request data to JSON format.
jsonData,err:=json.Marshal(data)iferr!=nil{panic(err)}// Send a POST request to the AIOS API's v1/chat/completions endpoint.
// This request asks the model to analyze the image.
response,err:=http.Post(aiosBaseUrl+"/v1/chat/completions","application/json",bytes.NewBuffer(jsonData))iferr!=nil{panic(err)}deferresponse.Body.Close()// Read the response body.
body,err:=io.ReadAll(response.Body)iferr!=nil{panic(err)}// Parse the response body to JSON format.
// This converts the model's response received from the server into structured data.
varvmap[string]interface{}json.Unmarshal(body,&v)// Print the response generated by the AI model.
// This response is the model's description of the image content.
choices:=v["choices"].([]interface{})choice:=choices[0].(map[string]interface{})message,err:=json.MarshalIndent(choice["message"],""," ")iferr!=nil{panic(err)}fmt.Println(string(message))}
package main
import (
"bytes""encoding/base64""encoding/json""fmt""io""net/http""os")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
var imagePath = "image/path.jpg"// Define the Message structure.
// Role: Message role (user, assistant, etc.)
// Content: Message content (including text and image URL)
type Message struct {
Role string`json:"role"` Content []map[string]interface{} `json:"content"`}
// Define the POST request data structure.
// Model: Model ID to use
// Messages: List of messages
// Stream: Whether to stream
type PostData struct {
Model string`json:"model"` Messages []Message `json:"messages"` Stream bool`json:"stream,omitempty"`}
// Define a function to Base64 encode an image file.
// This converts the image to text format so it can be transmitted to the API.
funcimageFileToBase64(imagePath string) (string, error) {
data, err := os.ReadFile(imagePath)
if err !=nil {
return"", err
}
return base64.StdEncoding.EncodeToString([]byte(data)), nil}
funcmain() {
// Encode the image file in Base64 format.
base64Image, err :=imageFileToBase64(imagePath)
if err !=nil {
panic(err)
}
// Configure the request data.
// In this example, the user is asked to ask a question about the image.
// The image is transmitted as a Base64-encoded string.
data := PostData{
Model: model,
Messages: []Message{
{
Role: "user",
Content: []map[string]interface{}{
{
"type": "text",
"text": "what's in this image?",
},
{
"type": "image_url",
"image_url": map[string]string{
"url": fmt.Sprintf("data:image/jpeg;base64,%s", base64Image),
},
},
},
},
},
}
// Serialize the request data to JSON format.
jsonData, err := json.Marshal(data)
if err !=nil {
panic(err)
}
// Send a POST request to the AIOS API's v1/chat/completions endpoint.
// This request asks the model to analyze the image.
response, err := http.Post(aiosBaseUrl+"/v1/chat/completions", "application/json", bytes.NewBuffer(jsonData))
if err !=nil {
panic(err)
}
defer response.Body.Close()
// Read the response body.
body, err := io.ReadAll(response.Body)
if err !=nil {
panic(err)
}
// Parse the response body to JSON format.
// This converts the model's response received from the server into structured data.
var v map[string]interface{}
json.Unmarshal(body, &v)
// Print the response generated by the AI model.
// This response is the model's description of the image content.
choices := v["choices"].([]interface{})
choice := choices[0].(map[string]interface{})
message, err := json.MarshalIndent(choice["message"], "", " ")
if err !=nil {
panic(err)
}
fmt.Println(string(message))
}
packagemainimport("context""encoding/base64""fmt""os""github.com/openai/openai-go""github.com/openai/openai-go/option")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)varimagePath="image/path.jpg"// Define a function to Base64 encode an image file.
// This converts the image to text format so it can be transmitted to the API.
funcimageFileToBase64(imagePathstring)(string,error){data,err:=os.ReadFile(imagePath)iferr!=nil{return"",err}returnbase64.StdEncoding.EncodeToString([]byte(data)),nil}funcmain(){// Encode the image file in Base64 format.
base64Image,err:=imageFileToBase64(imagePath)iferr!=nil{panic(err)}// Create an OpenAI client.
// base_url points to the v1 endpoint of the AIOS API.
client:=openai.NewClient(option.WithBaseURL(aiosBaseUrl+"/v1"),)// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// In this example, the user is asked to ask a question about the image.
// The image is transmitted as a Base64-encoded string.
response,err:=client.Chat.Completions.New(context.TODO(),openai.ChatCompletionNewParams{Model:model,Messages:[]openai.ChatCompletionMessageParamUnion{openai.UserMessage([]openai.ChatCompletionContentPartUnionParam{{OfText:&openai.ChatCompletionContentPartTextParam{Text:"what's in this image?",},},{OfImageURL:&openai.ChatCompletionContentPartImageParam{ImageURL:openai.ChatCompletionContentPartImageImageURLParam{URL:fmt.Sprintf("data:image/jpeg;base64,%s",base64Image),},},},}),},})iferr!=nil{panic(err)}// Print the response generated by the AI model.
// This response is the model's description of the image content.
fmt.Println(response.Choices[0].Message.RawJSON())}
package main
import (
"context""encoding/base64""fmt""os""github.com/openai/openai-go""github.com/openai/openai-go/option")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
var imagePath = "image/path.jpg"// Define a function to Base64 encode an image file.
// This converts the image to text format so it can be transmitted to the API.
funcimageFileToBase64(imagePath string) (string, error) {
data, err := os.ReadFile(imagePath)
if err !=nil {
return"", err
}
return base64.StdEncoding.EncodeToString([]byte(data)), nil}
funcmain() {
// Encode the image file in Base64 format.
base64Image, err :=imageFileToBase64(imagePath)
if err !=nil {
panic(err)
}
// Create an OpenAI client.
// base_url points to the v1 endpoint of the AIOS API.
client := openai.NewClient(
option.WithBaseURL(aiosBaseUrl +"/v1"),
)
// Generate a chat completion using the AIOS model.
// The model parameter specifies the model ID to use.
// The messages parameter is a list of messages containing the user message.
// In this example, the user is asked to ask a question about the image.
// The image is transmitted as a Base64-encoded string.
response, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
Model: model,
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage([]openai.ChatCompletionContentPartUnionParam{
{
OfText: &openai.ChatCompletionContentPartTextParam{
Text: "what's in this image?",
},
},
{
OfImageURL: &openai.ChatCompletionContentPartImageParam{
ImageURL: openai.ChatCompletionContentPartImageImageURLParam{
URL: fmt.Sprintf("data:image/jpeg;base64,%s", base64Image),
},
},
},
}),
},
})
if err !=nil {
panic(err)
}
// Print the response generated by the AI model.
// This response is the model's description of the image content.
fmt.Println(response.Choices[0].Message.RawJSON())
}
Code block. vision request
Response
The image is analyzed and text is generated as follows:
{'annotations':None,'audio':None,'content':"Here's what's in the image:\n"'\n''* **A Golden Retriever puppy:** The main focus is a cute, ''fluffy golden retriever puppy lying on a patch of grass.\n''* **A bone:** The puppy is chewing on a pink bone.\n''* **Green grass:** The puppy is lying on a vibrant green lawn.\n''* **Background:** There’s a bit of foliage and some elements of ''a garden or yard in the background, including a small shed and ''some plants.\n''\n'"It's a really heartwarming image!",'function_call':None,'reasoning_content':None,'refusal':None,'role':'assistant','tool_calls':[]}
Embeddings API
Embeddings converts input text into high-dimensional vectors of a specified dimension.
The generated vectors can be used for various natural language processing tasks such as text similarity, clustering, and search.
Request
Color mode
importjsonimportrequestsfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the data to pass to the model.data={"model":model,"input":"What is the capital of France?"}# Send a POST request to AIOS's /v1/embeddings API endpoint.response=requests.post(urljoin(aios_base_url,"v1/embeddings"),json=data)body=json.loads(response.text)# Print the generated embedding vector from the response.print(body["data"][0]["embedding"])
importjsonimportrequestsfromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the data to pass to the model.data = {
"model": model,
"input": "What is the capital of France?"}
# Send a POST request to AIOS's /v1/embeddings API endpoint.response = requests.post(urljoin(aios_base_url, "v1/embeddings"), json=data)
body = json.loads(response.text)
# Print the generated embedding vector from the response.print(body["data"][0]["embedding"])
fromopenaiimportOpenAIfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url specifies the AIOS API endpoint,# and api_key is set to a dummy value ("EMPTY_KEY").client=OpenAI(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY")# Call the OpenAI client's embeddings.create method to generate an embedding.# Pass the input text and model ID to generate an embedding vector.response=client.embeddings.create(input="What is the capital of France?",model=model)# Print the generated embedding vector.print(response.data[0].embedding)
fromopenaiimport OpenAI
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create an OpenAI client.# base_url specifies the AIOS API endpoint,# and api_key is set to a dummy value ("EMPTY_KEY").client = OpenAI(base_url=urljoin(aios_base_url, "v1"), api_key="EMPTY_KEY")
# Call the OpenAI client's embeddings.create method to generate an embedding.# Pass the input text and model ID to generate an embedding vector.response = client.embeddings.create(
input="What is the capital of France?",
model=model
)
# Print the generated embedding vector.print(response.data[0].embedding)
fromlangchain_togetherimportTogetherEmbeddingsfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create an embedding instance using the TogetherEmbeddings class.# base_url specifies the AIOS API endpoint,# api_key is set to a dummy value ("EMPTY_KEY").# model specifies the embedding model to use.embeddings=TogetherEmbeddings(base_url=urljoin(aios_base_url,"v1"),api_key="EMPTY_KEY",model=model)# Generate an embedding vector for the input text.# The embed_query method generates an embedding for a single sentence.embedding=embeddings.embed_query("What is the capital of France?")# Print the generated embedding vector.print(embedding)
fromlangchain_togetherimport TogetherEmbeddings
fromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create an embedding instance using the TogetherEmbeddings class.# base_url specifies the AIOS API endpoint,# api_key is set to a dummy value ("EMPTY_KEY").# model specifies the embedding model to use.embeddings = TogetherEmbeddings(
base_url=urljoin(aios_base_url, "v1"),
api_key="EMPTY_KEY",
model=model
)
# Generate an embedding vector for the input text.# The embed_query method generates an embedding for a single sentence.embedding = embeddings.embed_query("What is the capital of France?")
# Print the generated embedding vector.print(embedding)
constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Configure the data to pass to the model.
constdata={model:model,input:"What is the capital of France?"};// Generate the AIOS API's v1/embeddings endpoint URL.
leturl=newURL("/v1/embeddings",aios_base_url);// Send a POST request to AIOS's embeddings API endpoint.
constresponse=awaitfetch(url,{method:"POST",headers:{"Content-Type":"application/json",},body:JSON.stringify(data),});constbody=awaitresponse.json();// Print the generated embedding vector from the response.
console.log(body.data[0].embedding);
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Configure the data to pass to the model.
const data = {
model: model,
input:"What is the capital of France?"};
// Generate the AIOS API's v1/embeddings endpoint URL.
let url =new URL("/v1/embeddings", aios_base_url);
// Send a POST request to AIOS's embeddings API endpoint.
const response =await fetch(url, {
method:"POST",
headers: {
"Content-Type":"application/json",
},
body: JSON.stringify(data),
});
const body =await response.json();
// Print the generated embedding vector from the response.
console.log(body.data[0].embedding);
importOpenAIfrom"openai";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is set to a dummy value ("EMPTY_KEY"),
// and baseURL specifies the AIOS API endpoint.
constclient=newOpenAI({apiKey:"EMPTY_KEY",baseURL:newURL("v1",aios_base_url).href,});// Call the OpenAI client's embeddings.create method to generate an embedding.
// Pass the input text and model ID to generate an embedding vector.
constresponse=awaitclient.embeddings.create({model:model,input:"What is the capital of France?",});// Print the generated embedding vector.
console.log(response.data[0].embedding);
import OpenAI from "openai";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Create an OpenAI client.
// apiKey is set to a dummy value ("EMPTY_KEY"),
// and baseURL specifies the AIOS API endpoint.
const client =new OpenAI({
apiKey:"EMPTY_KEY",
baseURL:new URL("v1", aios_base_url).href,
});
// Call the OpenAI client's embeddings.create method to generate an embedding.
// Pass the input text and model ID to generate an embedding vector.
const response =await client.embeddings.create({
model: model,
input:"What is the capital of France?",
});
// Print the generated embedding vector.
console.log(response.data[0].embedding);
import{OpenAIEmbeddings}from"@langchain/openai";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Create an embedding instance using LangChain's OpenAIEmbeddings class.
// base_url points to the v1 endpoint of the AIOS API,
// and api_key is the key required by AIOS, typically set to "EMPTY_KEY".
// The model parameter specifies the model ID to use.
constembeddings=newOpenAIEmbeddings({model:model,apiKey:"EMPTY_KEY",configuration:{baseURL:newURL("v1",aios_base_url).href,},});// Generate an embedding vector for the input text.
// The embedQuery method generates an embedding for a single sentence.
constresponse=awaitembeddings.embedQuery("What is the capital of France?");// Print the generated embedding vector.
console.log(response);
import { OpenAIEmbeddings } from "@langchain/openai";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Create an embedding instance using LangChain's OpenAIEmbeddings class.
// base_url points to the v1 endpoint of the AIOS API,
// and api_key is the key required by AIOS, typically set to "EMPTY_KEY".
// The model parameter specifies the model ID to use.
const embeddings =new OpenAIEmbeddings({
model: model,
apiKey:"EMPTY_KEY",
configuration: {
baseURL:new URL("v1", aios_base_url).href,
},
});
// Generate an embedding vector for the input text.
// The embedQuery method generates an embedding for a single sentence.
const response =await embeddings.embedQuery("What is the capital of France?");
// Print the generated embedding vector.
console.log(response);
packagemainimport("bytes""encoding/json""fmt""io""net/http")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)// Define the data structure for the POST request.
// Model: Model ID to use
// Input: Input text to generate embedding for
typePostDatastruct{Modelstring`json:"model"`Inputstring`json:"input"`}funcmain(){// Create the request data.
data:=PostData{Model:model,Input:"What is the capital of France?",}// Marshal the data to JSON format.
jsonData,err:=json.Marshal(data)iferr!=nil{panic(err)}// Send a POST request to the AIOS API's v1/embeddings endpoint.
response,err:=http.Post(aiosBaseUrl+"/v1/embeddings","application/json",bytes.NewBuffer(jsonData))iferr!=nil{panic(err)}deferresponse.Body.Close()// Read the entire response body.
body,err:=io.ReadAll(response.Body)iferr!=nil{panic(err)}// Unmarshal the response body to map format.
varvmap[string]interface{}json.Unmarshal(body,&v)responseData:=v["data"].([]interface{})firstData:=responseData[0].(map[string]interface{})// Print the embedding vector of the first data in JSON format.
embedding,err:=json.MarshalIndent(firstData["embedding"],""," ")iferr!=nil{panic(err)}fmt.Println(string(embedding))}
package main
import (
"bytes""encoding/json""fmt""io""net/http")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
// Define the data structure for the POST request.
// Model: Model ID to use
// Input: Input text to generate embedding for
type PostData struct {
Model string`json:"model"` Input string`json:"input"`}
funcmain() {
// Create the request data.
data := PostData{
Model: model,
Input: "What is the capital of France?",
}
// Marshal the data to JSON format.
jsonData, err := json.Marshal(data)
if err !=nil {
panic(err)
}
// Send a POST request to the AIOS API's v1/embeddings endpoint.
response, err := http.Post(aiosBaseUrl+"/v1/embeddings", "application/json", bytes.NewBuffer(jsonData))
if err !=nil {
panic(err)
}
defer response.Body.Close()
// Read the entire response body.
body, err := io.ReadAll(response.Body)
if err !=nil {
panic(err)
}
// Unmarshal the response body to map format.
var v map[string]interface{}
json.Unmarshal(body, &v)
responseData := v["data"].([]interface{})
firstData := responseData[0].(map[string]interface{})
// Print the embedding vector of the first data in JSON format.
embedding, err := json.MarshalIndent(firstData["embedding"], "", " ")
if err !=nil {
panic(err)
}
fmt.Println(string(embedding))
}
packagemainimport("context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)funcmain(){// Create an OpenAI client.
// Use option.WithBaseURL to set the v1 endpoint of the AIOS API.
client:=openai.NewClient(option.WithBaseURL(aiosBaseUrl+"/v1"),)// Generate an embedding using the AIOS model.
// Use openai.EmbeddingNewParams to set the model and input text.
// The input text is "What is the capital of France?".
completion,err:=client.Embeddings.New(context.TODO(),openai.EmbeddingNewParams{Model:model,Input:openai.EmbeddingNewParamsInputUnion{OfString:openai.String("What is the capital of France?"),},})iferr!=nil{panic(err)}// Print the generated embedding vector.
fmt.Println(completion.Data[0].Embedding)}
package main
import (
"context""fmt""github.com/openai/openai-go""github.com/openai/openai-go/option")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
funcmain() {
// Create an OpenAI client.
// Use option.WithBaseURL to set the v1 endpoint of the AIOS API.
client := openai.NewClient(
option.WithBaseURL(aiosBaseUrl +"/v1"),
)
// Generate an embedding using the AIOS model.
// Use openai.EmbeddingNewParams to set the model and input text.
// The input text is "What is the capital of France?".
completion, err := client.Embeddings.New(context.TODO(), openai.EmbeddingNewParams{
Model: model,
Input: openai.EmbeddingNewParamsInputUnion{
OfString: openai.String("What is the capital of France?"),
},
})
if err !=nil {
panic(err)
}
// Print the generated embedding vector.
fmt.Println(completion.Data[0].Embedding)
}
Code block. /v1/embeddings request
Note
The aios endpoint-url and model ID information for calling the model are provided in the LLM Endpoint Usage Guide on the resource detail page. Please refer to Using LLM.
Response
You receive the value converted to vector format in embedding of data.
Rerank calculates the relevance to a query for given documents and assigns a ranking.
It helps improve the performance of RAG (Retrieval-Augmented Generation) applications by prioritizing relevant documents.
Request
Color mode
importjsonimportrequestsfromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# This includes the model ID to use, query, list of documents, and top N results.data={"model":model,"query":"What is the capital of France?","documents":["The capital of France is Paris.","France capital city is known for the Eiffel Tower.","Paris is located in the north-central part of France."],"top_n":3}# Send a POST request to the AIOS API's v2/rerank endpoint.# Compare the query and document list to rearrange documents with high relevance.response=requests.post(urljoin(aios_base_url,"v2/rerank"),json=data)body=json.loads(response.text)# Print the rearranged results.# This result is a list of documents sorted by relevance score between the query and documents.print(body["results"])
importjsonimportrequestsfromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Configure the request data.# This includes the model ID to use, query, list of documents, and top N results.data = {
"model": model,
"query": "What is the capital of France?",
"documents": [
"The capital of France is Paris.",
"France capital city is known for the Eiffel Tower.",
"Paris is located in the north-central part of France." ],
"top_n": 3}
# Send a POST request to the AIOS API's v2/rerank endpoint.# Compare the query and document list to rearrange documents with high relevance.response = requests.post(urljoin(aios_base_url, "v2/rerank"), json=data)
body = json.loads(response.text)
# Print the rearranged results.# This result is a list of documents sorted by relevance score between the query and documents.print(body["results"])
importcoherefromurllib.parseimporturljoinaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create a Cohere client.# api_key is the key required by AIOS, typically set to "EMPTY_KEY".# base_url points to the base path of the AIOS API.client=cohere.ClientV2("EMPTY_KEY",base_url=aios_base_url)# Define the list of documents.# These are the documents to search.docs=["The capital of France is Paris.","France capital city is known for the Eiffel Tower.","Paris is located in the north-central part of France."]# Use the AIOS model to rerank documents.# The model parameter specifies the model ID to use.# The query parameter is the search query.# The documents parameter is the list of documents to search.# The top_n parameter returns the top N results.response=client.rerank(model=model,query="What is the capital of France?",documents=docs,top_n=3,)# Print the rearranged results.# This result is a list of documents sorted by relevance score between the query and documents.print([result.model_dump()forresultinresponse.results])
importcoherefromurllib.parseimport urljoin
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create a Cohere client.# api_key is the key required by AIOS, typically set to "EMPTY_KEY".# base_url points to the base path of the AIOS API.client = cohere.ClientV2("EMPTY_KEY", base_url=aios_base_url)
# Define the list of documents.# These are the documents to search.docs = [
"The capital of France is Paris.",
"France capital city is known for the Eiffel Tower.",
"Paris is located in the north-central part of France."]
# Use the AIOS model to rerank documents.# The model parameter specifies the model ID to use.# The query parameter is the search query.# The documents parameter is the list of documents to search.# The top_n parameter returns the top N results.response = client.rerank(
model=model,
query="What is the capital of France?",
documents=docs,
top_n=3,
)
# Print the rearranged results.# This result is a list of documents sorted by relevance score between the query and documents.print([result.model_dump() for result in response.results])
fromlangchain_cohere.rerankimportCohereRerankaios_base_url="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model="<<model>>"# Enter the model ID for calling the AIOS model.# Create a reranker instance using the CohereRerank class.# base_url points to the base path of the AIOS API.# cohere_api_key is the key required for API requests, typically set to "EMPTY_KEY".# The model parameter specifies the model ID to use.rerank=CohereRerank(base_url=aios_base_url,cohere_api_key="EMPTY_KEY",model=model)# Define the list of documents.# These are the documents to rearrange.docs=["The capital of France is Paris.","France capital city is known for the Eiffel Tower.","Paris is located in the north-central part of France."]# Use the reranker to rearrange documents.# The documents parameter is the list of documents to rearrange.# The query parameter is the search query.# The top_n parameter returns the top N results.ranks=rerank.rerank(documents=docs,query="What is the capital of France?",top_n=3)# Print the rearranged results.# This result is a list of documents sorted by relevance score between the query and documents.print(ranks)
fromlangchain_cohere.rerankimport CohereRerank
aios_base_url ="<<aios endpoint-url>>"# Enter the aios endpoint-url for calling the AIOS model.model ="<<model>>"# Enter the model ID for calling the AIOS model.# Create a reranker instance using the CohereRerank class.# base_url points to the base path of the AIOS API.# cohere_api_key is the key required for API requests, typically set to "EMPTY_KEY".# The model parameter specifies the model ID to use.rerank = CohereRerank(
base_url=aios_base_url,
cohere_api_key="EMPTY_KEY",
model=model
)
# Define the list of documents.# These are the documents to rearrange.docs = [
"The capital of France is Paris.",
"France capital city is known for the Eiffel Tower.",
"Paris is located in the north-central part of France."]
# Use the reranker to rearrange documents.# The documents parameter is the list of documents to rearrange.# The query parameter is the search query.# The top_n parameter returns the top N results.ranks = rerank.rerank(
documents=docs,
query="What is the capital of France?",
top_n=3)
# Print the rearranged results.# This result is a list of documents sorted by relevance score between the query and documents.print(ranks)
constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Configure the request data.
// This includes the model ID to use, query, list of documents, and top N results.
constdata={model:model,query:"What is the capital of France?",documents:["The capital of France is Paris.","France capital city is known for the Eiffel Tower.","Paris is located in the north-central part of France.",],top_n:3,};// Generate the AIOS API's v2/rerank endpoint URL.
leturl=newURL("/v2/rerank",aios_base_url);// Send a POST request to the AIOS API.
// This endpoint rearranges documents with high relevance by comparing the query and document list.
constresponse=awaitfetch(url,{method:"POST",headers:{"Content-Type":"application/json",},body:JSON.stringify(data),});constbody=awaitresponse.json();// Print the rearranged results.
// This result is a list of documents sorted by relevance score between the query and documents.
console.log(body.results);
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Configure the request data.
// This includes the model ID to use, query, list of documents, and top N results.
const data = {
model: model,
query:"What is the capital of France?",
documents: [
"The capital of France is Paris.",
"France capital city is known for the Eiffel Tower.",
"Paris is located in the north-central part of France.",
],
top_n:3,
};
// Generate the AIOS API's v2/rerank endpoint URL.
let url =new URL("/v2/rerank", aios_base_url);
// Send a POST request to the AIOS API.
// This endpoint rearranges documents with high relevance by comparing the query and document list.
const response =await fetch(url, {
method:"POST",
headers: {
"Content-Type":"application/json",
},
body: JSON.stringify(data),
});
const body =await response.json();
// Print the rearranged results.
// This result is a list of documents sorted by relevance score between the query and documents.
console.log(body.results);
import{CohereClientV2}from"cohere-ai";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Create a CohereClientV2 client.
// token is the key required for API requests, typically set to "EMPTY_KEY".
// environment points to the base path of the AIOS API.
constcohere=newCohereClientV2({token:"EMPTY_KEY",environment:aios_base_url,});// Define the list of documents.
// These are the documents to rearrange.
constdocs=["The capital of France is Paris.","France capital city is known for the Eiffel Tower.","Paris is located in the north-central part of France.",];// Use the AIOS model to rearrange documents.
// The model parameter specifies the model ID to use.
// The query parameter is the search query.
// The documents parameter is the list of documents to rearrange.
// The topN parameter returns the top N results.
constresponse=awaitcohere.rerank({model:model,query:"What is the capital of France?",documents:docs,topN:3,});// Print the rearranged results.
// This result is a list of documents sorted by relevance score between the query and documents.
console.log(response.results);
import { CohereClientV2 } from "cohere-ai";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Create a CohereClientV2 client.
// token is the key required for API requests, typically set to "EMPTY_KEY".
// environment points to the base path of the AIOS API.
const cohere =new CohereClientV2({
token:"EMPTY_KEY",
environment: aios_base_url,
});
// Define the list of documents.
// These are the documents to rearrange.
const docs = [
"The capital of France is Paris.",
"France capital city is known for the Eiffel Tower.",
"Paris is located in the north-central part of France.",
];
// Use the AIOS model to rearrange documents.
// The model parameter specifies the model ID to use.
// The query parameter is the search query.
// The documents parameter is the list of documents to rearrange.
// The topN parameter returns the top N results.
const response =await cohere.rerank({
model: model,
query:"What is the capital of France?",
documents: docs,
topN:3,
});
// Print the rearranged results.
// This result is a list of documents sorted by relevance score between the query and documents.
console.log(response.results);
import{CohereClientV2}from"cohere-ai";import{CohereRerank}from"@langchain/cohere";constaios_base_url="<<aios endpoint-url>>";// Enter the aios endpoint-url for calling the AIOS model.
constmodel="<<model>>";// Enter the model ID for calling the AIOS model.
// Create a CohereClientV2 client.
// token is the key required for API requests, typically set to "EMPTY_KEY".
// environment points to the base path of the AIOS API.
constcohere=newCohereClientV2({token:"EMPTY_KEY",environment:aios_base_url,});// Create a rearranger instance using the CohereRerank class.
// The model parameter specifies the model ID to use.
// The client parameter passes the CohereClientV2 instance created above.
constreranker=newCohereRerank({model:model,client:cohere,});// Define the list of documents.
// These are the documents to rearrange.
constdocs=["The capital of France is Paris.","France capital city is known for the Eiffel Tower.","Paris is located in the north-central part of France.",];// Define the search query.
constquery="What is the capital of France?";// Use the rerank method of the reranker to rearrange documents.
// The first argument is the list of documents to rearrange.
// The second argument is the search query.
constresponse=awaitreranker.rerank(docs,query);// Print the rearranged results.
// This result is a list of documents sorted by relevance score between the query and documents.
console.log(response);
import { CohereClientV2 } from "cohere-ai";
import { CohereRerank } from "@langchain/cohere";
const aios_base_url ="<<aios endpoint-url>>"; // Enter the aios endpoint-url for calling the AIOS model.
const model ="<<model>>"; // Enter the model ID for calling the AIOS model.
// Create a CohereClientV2 client.
// token is the key required for API requests, typically set to "EMPTY_KEY".
// environment points to the base path of the AIOS API.
const cohere =new CohereClientV2({
token:"EMPTY_KEY",
environment: aios_base_url,
});
// Create a rearranger instance using the CohereRerank class.
// The model parameter specifies the model ID to use.
// The client parameter passes the CohereClientV2 instance created above.
const reranker =new CohereRerank({
model: model,
client: cohere,
});
// Define the list of documents.
// These are the documents to rearrange.
const docs = [
"The capital of France is Paris.",
"France capital city is known for the Eiffel Tower.",
"Paris is located in the north-central part of France.",
];
// Define the search query.
const query ="What is the capital of France?";
// Use the rerank method of the reranker to rearrange documents.
// The first argument is the list of documents to rearrange.
// The second argument is the search query.
const response =await reranker.rerank(docs, query);
// Print the rearranged results.
// This result is a list of documents sorted by relevance score between the query and documents.
console.log(response);
packagemainimport("bytes""encoding/json""fmt""io""net/http")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)// Define the data structure for the POST request.
// Model: Model ID to use
// Query: Search query
// Documents: List of documents to rearrange
// TopN: Return top N results
typePostDatastruct{Modelstring`json:"model"`Querystring`json:"query"`Documents[]string`json:"documents"`TopNint32`json:"top_n"`}funcmain(){// Create the request data.
// The query is "What is the capital of France?",
// and the document list consists of three sentences.
// TopN is set to 3 to return the top 3 results.
data:=PostData{Model:model,Query:"What is the capital of France?",Documents:[]string{"The capital of France is Paris.","France capital city is known for the Eiffel Tower.","Paris is located in the north-central part of France.",},TopN:3,}// Marshal the data to JSON format.
jsonData,err:=json.Marshal(data)iferr!=nil{panic(err)}// Send a POST request to the AIOS API's v2/rerank endpoint.
// Compare the query and document list to rearrange documents with high relevance.
response,err:=http.Post(aiosBaseUrl+"/v2/rerank","application/json",bytes.NewBuffer(jsonData))iferr!=nil{panic(err)}deferresponse.Body.Close()// Read the entire response body.
body,err:=io.ReadAll(response.Body)iferr!=nil{panic(err)}// Unmarshal the response body to map format.
varvmap[string]interface{}json.Unmarshal(body,&v)// Print the rearranged results in JSON format.
// This result is a list of documents sorted by relevance score between the query and documents.
rerank,err:=json.MarshalIndent(v["results"],""," ")iferr!=nil{panic(err)}fmt.Println(string(rerank))}
package main
import (
"bytes""encoding/json""fmt""io""net/http")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
// Define the data structure for the POST request.
// Model: Model ID to use
// Query: Search query
// Documents: List of documents to rearrange
// TopN: Return top N results
type PostData struct {
Model string`json:"model"` Query string`json:"query"` Documents []string`json:"documents"` TopN int32`json:"top_n"`}
funcmain() {
// Create the request data.
// The query is "What is the capital of France?",
// and the document list consists of three sentences.
// TopN is set to 3 to return the top 3 results.
data := PostData{
Model: model,
Query: "What is the capital of France?",
Documents: []string{
"The capital of France is Paris.",
"France capital city is known for the Eiffel Tower.",
"Paris is located in the north-central part of France.",
},
TopN: 3,
}
// Marshal the data to JSON format.
jsonData, err := json.Marshal(data)
if err !=nil {
panic(err)
}
// Send a POST request to the AIOS API's v2/rerank endpoint.
// Compare the query and document list to rearrange documents with high relevance.
response, err := http.Post(aiosBaseUrl+"/v2/rerank", "application/json", bytes.NewBuffer(jsonData))
if err !=nil {
panic(err)
}
defer response.Body.Close()
// Read the entire response body.
body, err := io.ReadAll(response.Body)
if err !=nil {
panic(err)
}
// Unmarshal the response body to map format.
var v map[string]interface{}
json.Unmarshal(body, &v)
// Print the rearranged results in JSON format.
// This result is a list of documents sorted by relevance score between the query and documents.
rerank, err := json.MarshalIndent(v["results"], "", " ")
if err !=nil {
panic(err)
}
fmt.Println(string(rerank))
}
packagemainimport("context""fmt"api"github.com/cohere-ai/cohere-go/v2"client"github.com/cohere-ai/cohere-go/v2/client")const(aiosBaseUrl="<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model="<<model>>"// Enter the model ID for calling the AIOS model.
)funcmain(){// Create a Cohere client.
// Use WithBaseURL to set the base path of the AIOS API.
co:=client.NewClient(client.WithBaseURL(aiosBaseUrl),)// Define the search query.
query:="What is the capital of France?"// Define the list of documents.
// These are the documents to rearrange.
docs:=[]string{"The capital of France is Paris.","France capital city is known for the Eiffel Tower.","Paris is located in the north-central part of France.",}// Use the AIOS model to rearrange documents.
// Use &api.V2RerankRequest to set the model, query, and document list.
resp,err:=co.V2.Rerank(context.TODO(),&api.V2RerankRequest{Model:model,Query:query,Documents:docs,},)iferr!=nil{panic(err)}// Print the rearranged results.
// This result is a list of documents sorted by relevance score between the query and documents.
fmt.Println(resp.Results)}
package main
import (
"context""fmt" api "github.com/cohere-ai/cohere-go/v2" client "github.com/cohere-ai/cohere-go/v2/client")
const (
aiosBaseUrl = "<<aios endpoint-url>>"// Enter the aios endpoint-url for calling the AIOS model.
model = "<<model>>"// Enter the model ID for calling the AIOS model.
)
funcmain() {
// Create a Cohere client.
// Use WithBaseURL to set the base path of the AIOS API.
co := client.NewClient(
client.WithBaseURL(aiosBaseUrl),
)
// Define the search query.
query :="What is the capital of France?"// Define the list of documents.
// These are the documents to rearrange.
docs := []string{
"The capital of France is Paris.",
"France capital city is known for the Eiffel Tower.",
"Paris is located in the north-central part of France.",
}
// Use the AIOS model to rearrange documents.
// Use &api.V2RerankRequest to set the model, query, and document list.
resp, err := co.V2.Rerank(
context.TODO(),
&api.V2RerankRequest{
Model: model,
Query: query,
Documents: docs,
},
)
if err !=nil {
panic(err)
}
// Print the rearranged results.
// This result is a list of documents sorted by relevance score between the query and documents.
fmt.Println(resp.Results)
}
Code block. /v2/rerank request
Note
The aios endpoint-url and model ID information for calling the model are provided in the LLM Endpoint Usage Guide on the resource detail page. Please refer to Using LLM.
Response
In results, you can check the documents sorted in order of high relevance to the query.
[{'document':{'text':'The capital of France is Paris.'},'index':0,'relevance_score':0.9999659061431885},{'document':{'text':'France capital city is known for the Eiffel Tower.'},'index':1,'relevance_score':0.9663000106811523},{'document':{'text':'Paris is located in the north-central part of France.'},'index':2,'relevance_score':0.7127546668052673}]