Skip to main content

Chat Completions

The SDK provides powerful chat completion capabilities with support for streaming, expert routing, and conversation context.

Basic Usage

import { chat } from "@cpod/sdk";

const response = await chat.complete({
message: "What is machine learning?",
});

console.log(response.content);

Streaming Responses

For real-time streaming output:

import { chat } from "@cpod/sdk";

for await (const chunk of chat.stream({ message: "Write a story" })) {
process.stdout.write(chunk.content);
}

React Streaming Example

import { useState } from "react";
import { chat } from "@cpod/sdk";

function ChatComponent() {
const [response, setResponse] = useState("");
const [loading, setLoading] = useState(false);

const sendMessage = async (message: string) => {
setLoading(true);
setResponse("");

for await (const chunk of chat.stream({ message })) {
setResponse((prev) => prev + chunk.content);
}

setLoading(false);
};

return (
<div>
<button onClick={() => sendMessage("Hello!")}>
Send
</button>
<div>{response}</div>
</div>
);
}

Expert Routing

Route messages to specialized AI experts:

import { chat, listExperts } from "@cpod/sdk";

// List available experts
const experts = await listExperts();
console.log(experts.map(e => e.name));

// Route to a specific expert
const response = await chat.complete({
message: "How should I price this product?",
expertId: "sales-expert-id",
});

Conversation Context

Maintain conversation history:

import { chat } from "@cpod/sdk";

// Continue an existing conversation
const response = await chat.complete({
message: "Tell me more about that",
chatId: "existing-chat-id",
});

// Start a new chat in a project
const response = await chat.complete({
message: "Analyze this document",
projectId: "project-123",
});

Advanced Options

import { chat } from "@cpod/sdk";

const response = await chat.complete({
message: "Write creative content",

// Model parameters
temperature: 0.8, // Higher = more creative
max_tokens: 2000, // Maximum response length

// Context
expertId: "writer", // Route to specific expert
projectId: "proj-123", // Associate with project
chatId: "chat-456", // Continue conversation
});

Response Structure

interface ChatResponse {
content: string; // The AI response text
chat_id: string; // Conversation ID
message_id: string; // This message's ID
expert_id?: string; // Expert that responded
tokens_used?: number; // Token count
}