API Usage Examples

Code examples for common use cases of the Model API

Getting Started

This page provides code examples for common use cases of the Model API in various programming languages. You can use these examples as a starting point for integrating the API into your applications.

Note: These examples connect to a local server running at localhost:11435 with no authentication. If your deployment uses a different endpoint or requires authentication, adjust the examples accordingly.

Basic Chat Completion

Send a simple chat message and receive a completion:

import requests
import json

def chat_completion():
    url = "http://localhost:11435/v1/chat/completions"
    headers = {
        "Content-Type": "application/json"
    }
    
    data = {
        "model": "your-model-id",
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Hello, who are you?"}
        ],
        "temperature": 0.7
    }
    
    response = requests.post(url, headers=headers, json=data)
    
    if response.status_code == 200:
        result = response.json()
        # Extract the assistant's message
        assistant_message = result["choices"][0]["message"]["content"]
        print(f"Assistant: {assistant_message}")
    else:
        print(f"Error: {response.status_code}")
        print(response.text)

if __name__ == "__main__":
    chat_completion()
async function chatCompletion() {
  const url = 'http://localhost:11435/v1/chat/completions';
  
  const headers = {
    'Content-Type': 'application/json'
  };
  
  const data = {
    model: 'your-model-id',
    messages: [
      { role: 'system', content: 'You are a helpful assistant.' },
      { role: 'user', content: 'Hello, who are you?' }
    ],
    temperature: 0.7
  };
  
  try {
    const response = await fetch(url, {
      method: 'POST',
      headers: headers,
      body: JSON.stringify(data)
    });
    
    if (response.ok) {
      const result = await response.json();
      // Extract the assistant's message
      const assistantMessage = result.choices[0].message.content;
      console.log(`Assistant: ${assistantMessage}`);
    } else {
      console.error(`Error: ${response.status}`);
      console.error(await response.text());
    }
  } catch (error) {
    console.error('Request failed:', error);
  }
}

chatCompletion();
curl -X POST http://localhost:11435/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "your-model-id",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello, who are you?"}
    ],
    "temperature": 0.7
  }'

Streaming Chat Completion

Generate a chat completion with streaming responses:

import requests
import json

def stream_chat_completion():
    url = "http://localhost:11435/v1/chat/completions"
    headers = {
        "Content-Type": "application/json"
    }
    
    data = {
        "model": "your-model-id",
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Write a short poem about AI."}
        ],
        "temperature": 0.7,
        "stream": True
    }
    
    # Make a streaming request
    response = requests.post(url, headers=headers, json=data, stream=True)
    
    if response.status_code == 200:
        # Process the stream
        for line in response.iter_lines():
            if line:
                line = line.decode('utf-8')
                if line.startswith('data: '):
                    data_str = line[6:]  # Remove 'data: ' prefix
                    
                    # Check for stream end
                    if data_str == '[DONE]':
                        break
                    
                    try:
                        data_json = json.loads(data_str)
                        content = data_json['choices'][0]['delta'].get('content', '')
                        if content:
                            print(content, end='', flush=True)
                    except json.JSONDecodeError:
                        print(f"Error parsing JSON: {data_str}")
        print()  # Add a newline at the end
    else:
        print(f"Error: {response.status_code}")
        print(response.text)

if __name__ == "__main__":
    stream_chat_completion()
async function streamChatCompletion() {
  const url = 'http://localhost:11435/v1/chat/completions';
  
  const headers = {
    'Content-Type': 'application/json'
  };
  
  const data = {
    model: 'your-model-id',
    messages: [
      { role: 'system', content: 'You are a helpful assistant.' },
      { role: 'user', content: 'Write a short poem about AI.' }
    ],
    temperature: 0.7,
    stream: true
  };
  
  try {
    const response = await fetch(url, {
      method: 'POST',
      headers: headers,
      body: JSON.stringify(data)
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    // Process the stream
    const reader = response.body.getReader();
    const decoder = new TextDecoder('utf-8');
    let buffer = '';
    
    while (true) {
      const { done, value } = await reader.read();
      if (done) break;
      
      // Decode the received chunk and add it to the buffer
      buffer += decoder.decode(value, { stream: true });
      
      // Process complete lines from the buffer
      const lines = buffer.split('\n');
      buffer = lines.pop() || ''; // Keep any incomplete line in the buffer
      
      for (const line of lines) {
        if (line.startsWith('data: ')) {
          const data = line.slice(6); // Remove 'data: ' prefix
          
          // Check for stream end
          if (data === '[DONE]') {
            console.log('\nStream completed');
            break;
          }
          
          try {
            const jsonData = JSON.parse(data);
            const content = jsonData.choices[0]?.delta?.content || '';
            if (content) {
              process.stdout.write(content); // Print without newline in Node.js
              // For browser: document.getElementById('output').append(content);
            }
          } catch (error) {
            console.error('Error parsing JSON:', error);
          }
        }
      }
    }
  } catch (error) {
    console.error('Request failed:', error);
  }
}

streamChatCompletion();
curl -X POST http://localhost:11435/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "your-model-id",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Write a short poem about AI."}
    ],
    "temperature": 0.7,
    "stream": true
  }'

Using Function Calling

Use tools (function calling) to enable the model to call specific functions:

import requests
import json

def function_calling_example():
    url = "http://localhost:11435/v1/chat/completions"
    headers = {
        "Content-Type": "application/json"
    }
    
    data = {
        "model": "your-model-id",
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "What's the weather like in San Francisco?"}
        ],
        "tools": [
            {
                "type": "function",
                "function": {
                    "name": "get_current_weather",
                    "description": "Get the current weather in a given location",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "location": {
                                "type": "string",
                                "description": "The city and state, e.g. San Francisco, CA"
                            },
                            "unit": {
                                "type": "string",
                                "enum": ["celsius", "fahrenheit"],
                                "description": "The temperature unit to use"
                            }
                        },
                        "required": ["location"]
                    }
                }
            }
        ],
        "temperature": 0.7
    }
    
    response = requests.post(url, headers=headers, json=data)
    
    if response.status_code == 200:
        result = response.json()
        
        # Check if the model wants to call a function
        assistant_message = result["choices"][0]["message"]
        finish_reason = result["choices"][0]["finish_reason"]
        
        if finish_reason == "tool_calls":
            print("The model wants to call a function:")
            tool_calls = assistant_message["tool_calls"]
            
            for tool_call in tool_calls:
                function_name = tool_call["function"]["name"]
                function_args = json.loads(tool_call["function"]["arguments"])
                
                print(f"Function: {function_name}")
                print(f"Arguments: {function_args}")
                
                # In a real application, you would call your actual function here
                # For example:
                if function_name == "get_current_weather":
                    # Simulate getting weather data
                    weather_result = {
                        "location": function_args["location"],
                        "temperature": 22,
                        "unit": function_args.get("unit", "celsius"),
                        "forecast": ["sunny", "windy"]
                    }
                    
                    # Now send the function result back to the API
                    messages = data["messages"] + [
                        assistant_message,
                        {
                            "role": "tool",
                            "tool_call_id": tool_call["id"],
                            "name": function_name,
                            "content": json.dumps(weather_result)
                        }
                    ]
                    
                    # Make a new request with the updated messages
                    follow_up_data = data.copy()
                    follow_up_data["messages"] = messages
                    del follow_up_data["tools"]  # Remove tools to get a final answer
                    
                    follow_up_response = requests.post(url, headers=headers, json=follow_up_data)
                    
                    if follow_up_response.status_code == 200:
                        follow_up_result = follow_up_response.json()
                        final_response = follow_up_result["choices"][0]["message"]["content"]
                        print(f"\nFinal response: {final_response}")
                    else:
                        print(f"Error in follow-up request: {follow_up_response.status_code}")
        else:
            # The model provided a direct response
            print(f"Assistant: {assistant_message['content']}")
    else:
        print(f"Error: {response.status_code}")
        print(response.text)

if __name__ == "__main__":
    function_calling_example()
async function functionCallingExample() {
  const url = 'http://localhost:11435/v1/chat/completions';
  
  const headers = {
    'Content-Type': 'application/json'
  };
  
  const data = {
    model: 'your-model-id',
    messages: [
      { role: 'system', content: 'You are a helpful assistant.' },
      { role: 'user', content: "What's the weather like in San Francisco?" }
    ],
    tools: [
      {
        type: 'function',
        function: {
          name: 'get_current_weather',
          description: 'Get the current weather in a given location',
          parameters: {
            type: 'object',
            properties: {
              location: {
                type: 'string',
                description: 'The city and state, e.g. San Francisco, CA'
              },
              unit: {
                type: 'string',
                enum: ['celsius', 'fahrenheit'],
                description: 'The temperature unit to use'
              }
            },
            required: ['location']
          }
        }
      }
    ],
    temperature: 0.7
  };
  
  try {
    const response = await fetch(url, {
      method: 'POST',
      headers: headers,
      body: JSON.stringify(data)
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    const result = await response.json();
    
    // Check if the model wants to call a function
    const assistantMessage = result.choices[0].message;
    const finishReason = result.choices[0].finish_reason;
    
    if (finishReason === 'tool_calls') {
      console.log('The model wants to call a function:');
      const toolCalls = assistantMessage.tool_calls;
      
      for (const toolCall of toolCalls) {
        const functionName = toolCall.function.name;
        const functionArgs = JSON.parse(toolCall.function.arguments);
        
        console.log(`Function: ${functionName}`);
        console.log(`Arguments:`, functionArgs);
        
        // In a real application, you would call your actual function here
        // For example:
        if (functionName === 'get_current_weather') {
          // Simulate getting weather data
          const weatherResult = {
            location: functionArgs.location,
            temperature: 22,
            unit: functionArgs.unit || 'celsius',
            forecast: ['sunny', 'windy']
          };
          
          // Now send the function result back to the API
          const messages = [
            ...data.messages,
            assistantMessage,
            {
              role: 'tool',
              tool_call_id: toolCall.id,
              name: functionName,
              content: JSON.stringify(weatherResult)
            }
          ];
          
          // Make a new request with the updated messages
          const followUpData = { ...data, messages };
          delete followUpData.tools;  // Remove tools to get a final answer
          
          const followUpResponse = await fetch(url, {
            method: 'POST',
            headers: headers,
            body: JSON.stringify(followUpData)
          });
          
          if (followUpResponse.ok) {
            const followUpResult = await followUpResponse.json();
            const finalResponse = followUpResult.choices[0].message.content;
            console.log(`\nFinal response: ${finalResponse}`);
          } else {
            console.error(`Error in follow-up request: ${followUpResponse.status}`);
          }
        }
      }
    } else {
      // The model provided a direct response
      console.log(`Assistant: ${assistantMessage.content}`);
    }
  } catch (error) {
    console.error('Request failed:', error);
  }
}

functionCallingExample();