### Install Langbase SDK (npm, pnpm, yarn) Source: https://github.com/langbaseinc/langbase-sdk/blob/main/README.md Installs the Langbase SDK package using different package managers. This is the first step to using the SDK in your project. ```bash npm install langbase ``` ```bash pnpm add langbase ``` ```bash yarn add langbase ``` -------------------------------- ### Create and Configure a Custom Pipe with TypeScript Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt This example demonstrates how to create custom AI pipes with specific models, parameters, and configurations using the Langbase SDK. It shows setting the model, temperature, max tokens, top_p, stream settings, system messages, and variables for a pipe. Dependencies include 'dotenv/config' and 'langbase'. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { const pipe = await langbase.pipes.create({ name: 'customer-support-bot', description: 'AI assistant for customer support', status: 'private', model: 'openai:gpt-4o', temperature: 0.7, max_tokens: 1000, top_p: 0.9, stream: true, messages: [ { role: 'system', content: 'You are a helpful customer support assistant. Be concise and friendly.', }, ], variables: [ { name: 'company_name', value: 'Acme Corp', }, ], }); console.log('Pipe created:', pipe.name); console.log('API Key:', pipe.apiKey); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Configure Langbase Docs MCP Server for Cursor IDE Source: https://github.com/langbaseinc/langbase-sdk/blob/main/packages/cli/README.md This JSON configuration allows you to integrate the Langbase Docs MCP server into the Cursor IDE. It specifies the command and arguments needed to run the server. Ensure you have '@langbase/cli' installed. ```json { "mcpServers": { "Langbase": { "command": "npx", "args": ["@langbase/cli","docs-mcp-server"] } } } ``` -------------------------------- ### LLM Tool Calling and Function Execution with TypeScript Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt This example demonstrates how to enable Large Language Models (LLMs) to call external functions for enhanced capabilities. It sets up a Langbase client, defines a mock weather function, and configures a pipe to identify and execute tool calls based on user input. Dependencies include 'dotenv/config' and 'langbase'. ```typescript import 'dotenv/config'; import { Langbase, getToolsFromRun } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function getCurrentWeather(location: string, unit: string = 'celsius') { // Mock implementation - replace with real weather API return { location, temperature: 22, unit, conditions: 'Sunny', }; } async function main() { try { const response = await langbase.pipes.run({ name: 'summary', stream: false, messages: [ { 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', required: ['location'], properties: { location: { type: 'string', description: 'The city and state, e.g. San Francisco, CA', }, unit: { type: 'string', enum: ['celsius', 'fahrenheit'], default: 'celsius', }, }, }, }, }, ], }); // Extract tool calls from response const toolCalls = await getToolsFromRun(response); if (toolCalls.length > 0) { console.log('Tool calls requested:', toolCalls.length); for (const toolCall of toolCalls) { const args = JSON.parse(toolCall.function.arguments); console.log(`Calling ${toolCall.function.name} with:`, args); const weatherData = await getCurrentWeather( args.location, args.unit ); console.log('Weather data:', weatherData); } } else { console.log('Response:', response.completion); } } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Stream Agent Responses in Real-Time (TypeScript) Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Shows how to enable streaming output from a Langbase agent and process the data incrementally using the built‑in runner. The example writes each content chunk to stdout and signals completion, with error handling for robustness. ```typescript import 'dotenv/config'; import { Langbase, getRunner } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { const response = await langbase.agent.run({ model: 'openai:gpt-4o-mini', apiKey: process.env.OPENAI_API_KEY!, instructions: 'You are a creative writer.', input: 'Write a haiku about artificial intelligence.', stream: true, }); const runner = getRunner(response.stream); runner.on('content', (content) => { process.stdout.write(content); }); runner.on('end', () => { console.log('\n\nDone!'); }); runner.on('error', (error) => { console.error('Error:', error.message); }); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Structured Output with JSON Schema using TypeScript and Zod Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt This example shows how to generate structured JSON responses validated against a schema using Zod. It defines a Zod schema, converts it to a JSON schema, and configures a Langbase pipe to enforce this structure. The response is then parsed and validated against the original Zod schema. Dependencies include 'dotenv/config', 'langbase', 'zod', and 'zod-to-json-schema'. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; import { z } from 'zod'; import { zodToJsonSchema } from 'zod-to-json-schema'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); // Define schema const MathReasoningSchema = z.object({ steps: z.array( z.object({ explanation: z.string(), output: z.string(), }) ), final_answer: z.string(), }); async function main() { try { // Convert Zod schema to JSON Schema const jsonSchema = zodToJsonSchema(MathReasoningSchema, { target: 'openAi', }); // Create pipe with structured output await langbase.pipes.create({ name: 'math-tutor', model: 'openai:gpt-4o', json: true, response_format: { type: 'json_schema', json_schema: { name: 'math_reasoning', schema: jsonSchema, strict: true, }, }, }); // Run pipe const { completion } = await langbase.pipes.run({ name: 'math-tutor', messages: [ { role: 'user', content: 'How can I solve 8x + 22 = -23?', }, ], stream: false, }); // Parse and validate response const solution = MathReasoningSchema.parse(JSON.parse(completion)); console.log('Solution steps:'); solution.steps.forEach((step, i) => { console.log(`${i + 1}. ${step.explanation}`); console.log(` Result: ${step.output}`); }); console.log(` Final answer: ${solution.final_answer}`); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Initialize Langbase SDK (TypeScript) Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Demonstrates importing required modules, loading environment variables, and creating a Langbase client with an API key. Requires the dotenv package for configuration. The snippet produces a ready‑to‑use Langbase instance. ```TypeScript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY! }); ``` -------------------------------- ### Configure Langbase API Key Source: https://github.com/langbaseinc/langbase-sdk/blob/main/README.md Sets up the LANGBASE_API_KEY in a `.env` file, which is required for authenticating with the Langbase API. Ensure this key is kept secure. ```bash # Add your Langbase API key here: https://langbase.com/docs/api-reference/api-keys LANGBASE_API_KEY="your-api-key" ``` -------------------------------- ### Run Agent with Custom Instructions (TypeScript) Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Executes a Langbase agent with a specified model, API key, and custom instructions for a math tutoring scenario. The snippet logs the agent's output and token usage, handling errors gracefully. Suitable for synchronous response handling. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { const response = await langbase.agent.run({ model: 'openai:gpt-4o-mini', apiKey: process.env.OPENAI_API_KEY!, instructions: 'You are a helpful math tutor. Explain concepts clearly and provide examples.', input: 'Explain the Pythagorean theorem and give an example.', stream: false, temperature: 0.7, max_tokens: 500, }); console.log('Response:', response.output); console.log('Tokens used:', response.usage?.total_tokens); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Tool Calling and Function Execution Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Learn how to enable Large Language Models (LLMs) to call external functions and tools for enhanced capabilities. This section covers setting up the Langbase SDK, defining tools, and processing tool calls from LLM responses. ```APIDOC ## POST /pipes/run ### Description Executes a Langbase pipe, allowing the LLM to call predefined tools or functions based on the user's input. ### Method POST ### Endpoint /pipes/run ### Parameters #### Query Parameters - **name** (string) - Required - The name of the pipe to run. - **stream** (boolean) - Optional - Whether to stream the response. #### Request Body - **messages** (array) - Required - An array of message objects representing the conversation history. - **role** (string) - Required - The role of the message sender (e.g., 'user', 'assistant'). - **content** (string) - Required - The content of the message. - **tools** (array) - Optional - An array of tool definitions the LLM can use. - **type** (string) - Required - The type of tool (e.g., 'function'). - **function** (object) - Required - The function definition. - **name** (string) - Required - The name of the function. - **description** (string) - Optional - A description of what the function does. - **parameters** (object) - Required - The parameters the function accepts. - **type** (string) - Required - The type of the parameter object (usually 'object'). - **required** (array) - Optional - An array of required parameter names. - **properties** (object) - Required - An object defining the properties of the parameters. - **param_name** (object) - Required - Definition of a specific parameter. - **type** (string) - Required - The data type of the parameter (e.g., 'string', 'number', 'boolean'). - **description** (string) - Optional - A description of the parameter. - **enum** (array) - Optional - Allowed values for the parameter. - **default** (any) - Optional - The default value for the parameter. ### Request Example ```json { "name": "summary", "stream": false, "messages": [ { "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", "required": ["location"], "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "default": "celsius" } } } } } ] } ``` ### Response #### Success Response (200) - **completion** (string) - The LLM's response or the result of a tool call. - **tool_calls** (array) - An array of tool calls if the LLM decided to use a tool. - **id** (string) - Unique identifier for the tool call. - **type** (string) - Type of the call (usually 'function'). - **function** (object) - Details of the function call. - **name** (string) - The name of the function to call. - **arguments** (string) - A JSON string representing the arguments for the function call. #### Response Example ```json { "completion": null, "tool_calls": [ { "id": "call_abc123", "type": "function", "function": { "name": "get_current_weather", "arguments": "{\"location\": \"San Francisco, CA\", \"unit\": \"celsius\"}" } } ] } ``` ``` -------------------------------- ### Configure Langbase Docs MCP Server for Windsurf Source: https://github.com/langbaseinc/langbase-sdk/blob/main/packages/cli/README.md This JSON configuration is used to add the Langbase Docs MCP server to Windsurf. It defines the command and arguments for launching the server. Make sure '@langbase/cli' is available in your environment. ```json { "mcpServers": { "Langbase": { "command": "npx", "args": ["@langbase/cli", "docs-mcp-server"] } } } ``` -------------------------------- ### Create and Configure a Pipe Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Create custom AI pipes with specific models, parameters, and configurations to tailor LLM behavior for various tasks. ```APIDOC ## POST /pipes ### Description Creates a new custom pipe with detailed configurations, including model selection, system messages, and customizable variables. ### Method POST ### Endpoint /pipes ### Parameters #### Request Body - **name** (string) - Required - The name of the pipe. - **description** (string) - Optional - A description of the pipe's purpose. - **status** (string) - Optional - The status of the pipe (e.g., 'private', 'public'). - **model** (string) - Optional - The language model to use (e.g., 'openai:gpt-4o'). - **temperature** (number) - Optional - Controls randomness; lower values make output more focused. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate. - **top_p** (number) - Optional - Nucleus sampling parameter. - **stream** (boolean) - Optional - Whether to stream the response. - **messages** (array) - Optional - An array of initial message objects, typically including a system message. - **role** (string) - Required - The role of the message sender ('system', 'user', 'assistant'). - **content** (string) - Required - The content of the message. - **variables** (array) - Optional - An array of variables that can be used within the pipe. - **name** (string) - Required - The name of the variable. - **value** (string) - Required - The value of the variable. ### Request Example ```json { "name": "customer-support-bot", "description": "AI assistant for customer support", "status": "private", "model": "openai:gpt-4o", "temperature": 0.7, "max_tokens": 1000, "top_p": 0.9, "stream": true, "messages": [ { "role": "system", "content": "You are a helpful customer support assistant. Be concise and friendly." } ], "variables": [ { "name": "company_name", "value": "Acme Corp" } ] } ``` ### Response #### Success Response (200) - **pipe** (object) - Details of the created pipe. - **name** (string) - The name of the pipe. - **apiKey** (string) - The API key associated with the pipe. #### Response Example ```json { "pipe": { "name": "customer-support-bot", "apiKey": "pipe_abc456" } } ``` ``` -------------------------------- ### Generate Text with Langbase SDK (TypeScript) Source: https://github.com/langbaseinc/langbase-sdk/blob/main/README.md Demonstrates how to generate text using the `langbase.pipes.run()` method with `stream: false`. It requires an API key and a pipe name, then logs the response. ```typescript import 'dotenv/config'; import {Langbase} from 'langbase'; // 1. Initiate the Langbase. const langbase = new Langbase({ // Make sure you have a .env file with LANGBASE_API_KEY. apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { // 2. Run the pipe with a question. const response = await langbase.pipes.run({ stream: false, name: 'summary' // pipe name to run messages: [ { role: 'user', content: 'Who is an AI Engineer?', }, ], }); // 3. Print the response. console.log('response: ', response); } main(); ``` -------------------------------- ### Stream Text with Langbase SDK (TypeScript) Source: https://github.com/langbaseinc/langbase-sdk/blob/main/README.md Demonstrates how to stream text using the `langbase.pipes.run()` method with `stream: true`. It sets up a runner to listen for and print content chunks as they arrive. ```typescript import 'dotenv/config'; import {getRunner, Langbase} from 'langbase'; // 1. Initiate the Langbase. const langbase = new Langbase({ // Make sure you have a .env file with LANGBASE_API_KEY. apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { const userMsg = 'Who is an AI Engineer?'; // 2. Run the pipe with a question. const {stream} = await langbase.pipes.run({ stream: true, name: 'summary', // pipe name to run messages: [{role: 'user', content: userMsg}], }); // 3. Get the runner and listen to the content. const runner = getRunner(stream); // 4. Print the response. runner.on('content', content => { process.stdout.write(content); }); } main(); ``` -------------------------------- ### Run Agent with MCP Server Integration (TypeScript) Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Demonstrates how to integrate a Model Context Protocol (MCP) server into a Langbase agent request, enabling advanced tool capabilities. The code sends a user query to the agent and logs the response, handling any errors. Useful for extending agent functionality with external services. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { const response = await langbase.agent.run({ model: 'openai:gpt-4o-mini', apiKey: process.env.OPENAI_API_KEY!, mcp_servers: [ { type: 'url', name: 'deepwiki', url: 'https://mcp.deepwiki.com/sse', }, ], instructions: 'You are a helpful assistant that helps users research topics using available tools.', input: [ { role: 'user', content: 'What transport protocols does the 2025-03-26 version of the MCP spec support?', }, ], stream: false, }); console.log('Response:', response.output); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Generate Images with OpenAI via Langbase SDK Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Generates an image using OpenAI's models via the Langbase SDK. Requires Langbase and OpenAI API keys. Takes a text prompt and returns an image URL, with an option to download the image. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; import fs from 'fs'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { const result = await langbase.images.generate({ prompt: 'A serene landscape with mountains at sunset, digital art style', model: 'openai:gpt-image-1', apiKey: process.env.OPENAI_API_KEY!, width: 1024, height: 1024, n: 1, }); console.log('Image generated successfully'); console.log('Provider:', result.provider); console.log('Model:', result.model); const imageUrl = result.choices[0].message.images[0].image_url.url; console.log('Image URL:', imageUrl); // Optionally download image if (imageUrl.startsWith('http')) { const response = await fetch(imageUrl); const buffer = await response.arrayBuffer(); fs.writeFileSync('./generated-image.png', Buffer.from(buffer)); console.log('Image saved to ./generated-image.png'); } } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Create Memory Store for Semantic Search (TypeScript) Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Illustrates the creation of a memory store using Langbase, configured for document storage and semantic search with an OpenAI embedding model. The snippet logs the created memory's name and embedding model, and captures any errors during initialization. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { const memory = await langbase.memories.create({ name: 'company-docs', description: 'Internal company documentation', embedding_model: 'openai:text-embedding-3-large', top_k: 10, chunk_size: 1024, chunk_overlap: 256, }); console.log('Memory created:', memory.name); console.log('Embedding model:', memory.embedding_model); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Create Multi-Step AI Workflow with Retry Logic (TypeScript) Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Orchestrates a multi-step AI workflow including web searching, content outlining with retries, and content writing using Langbase SDK. It handles asynchronous operations, error catching, and streaming data for content generation. Dependencies include 'dotenv' for environment variables and 'langbase'. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { const workflow = langbase.workflow({ name: 'Content Generation Pipeline', debug: true, }); try { // Step 1: Research topic const research = await workflow.step({ id: 'research-topic', timeout: 30000, run: async () => { console.log('Researching topic...'); return langbase.tools.webSearch({ service: 'exa', query: 'Latest AI developments in 2024', totalResults: 3, apiKey: process.env.EXA_API_KEY!, }); }, }); console.log(`Found ${research.length} research sources`); // Step 2: Generate outline with retry const outline = await workflow.step({ id: 'generate-outline', timeout: 15000, retries: { limit: 3, delay: 1000, backoff: 'exponential', }, run: async () => { console.log('Generating outline...'); const response = await langbase.agent.run({ model: 'openai:gpt-4o-mini', apiKey: process.env.OPENAI_API_KEY!, instructions: 'Create a detailed outline for a blog post.', input: `Create an outline based on: ${research.map(r => r.content).join('\n\n')}`, stream: false, }); return response.output; }, }); console.log('Outline generated'); // Step 3: Write content const content = await workflow.step({ id: 'write-content', timeout: 30000, run: async () => { console.log('Writing content...'); const { stream } = await langbase.pipes.run({ name: 'content-writer', stream: true, messages: [ { role: 'system', content: 'You are a professional content writer.', }, { role: 'user', content: `Write a blog post following this outline:\n${outline}`, }, ], }); let fullContent = ''; const reader = stream.getReader(); const decoder = new TextDecoder(); while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = decoder.decode(value); const lines = chunk.split('\n').filter(line => line.trim()); for (const line of lines) { if (line.startsWith('data: ')) { const data = line.slice(6); if (data !== '[DONE]') { try { const parsed = JSON.parse(data); if (parsed.choices?.[0]?.delta?.content) { fullContent += parsed.choices[0].delta.content; } } catch (e) { // Skip parsing errors } } } } } return fullContent; }, }); console.log('\nFinal content length:', content.length, 'characters'); console.log('Content preview:', content.substring(0, 200) + '...'); } catch (error) { console.error('Workflow error:', error.message); } finally { // Always end workflow to send traces await workflow.end(); console.log('\nWorkflow completed'); } } main(); ``` -------------------------------- ### RAG with Memory and Pipes Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Combines memory retrieval with pipe execution for RAG applications. It retrieves relevant documents from memory based on a query and then uses this context within a specified pipe for generating responses. Requires Langbase API key and a configured 'qa-assistant' pipe. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { // Retrieve relevant documents const context = await langbase.memories.retrieve({ memory: [{ name: 'company-docs' }], query: 'What are the remote work guidelines?', topK: 3, }); // Use retrieved context in pipe const response = await langbase.pipes.run({ name: 'qa-assistant', stream: false, messages: [ { role: 'system', content: `You are a helpful assistant. Use the following context to answer questions:\n\n${context.map(c => c.text).join('\n\n')}`, }, { role: 'user', content: 'Can I work remotely 3 days a week?', }, ], }); console.log('Answer:', response.completion); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Generate Images with Different AI Providers via Langbase SDK Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Generates images using alternative AI providers like Together AI and Google Imagen through the Langbase SDK. Requires respective API keys and specifies different models and parameters. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { // Together AI const togetherResult = await langbase.images.generate({ prompt: 'Futuristic cityscape with flying cars, cyberpunk aesthetic', model: 'together:black-forest-labs/FLUX.1-schnell-Free', apiKey: process.env.TOGETHER_API_KEY!, width: 1024, height: 768, steps: 4, negative_prompt: 'blurry, low quality', }); console.log('Together AI image:', togetherResult.choices[0].message.images[0].image_url.url); // Google Imagen const googleResult = await langbase.images.generate({ prompt: 'A peaceful zen garden with cherry blossoms', model: 'google:imagen-3.0-generate-001', apiKey: process.env.GOOGLE_API_KEY!, width: 1024, height: 1024, }); console.log('Google Imagen:', googleResult.choices[0].message.images[0].image_url.url); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Web Crawling with Langbase SDK Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Crawls specified URLs to extract content. Requires API keys for Langbase and the crawling service. Outputs an array of crawled page objects, each containing URL and content. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { const results = await langbase.tools.crawl({ url: [ 'https://langbase.com', 'https://langbase.com/docs', 'https://langbase.com/about', ], maxPages: 3, service: 'spider', apiKey: process.env.CRAWL_KEY!, }); console.log(`Crawled ${results.length} pages:\n`); results.forEach((page, i) => { console.log(`${i + 1}. ${page.url}`); console.log(` Content length: ${page.content.length} characters`); console.log(` Preview: ${page.content.substring(0, 150)}...`); console.log(); }); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Next.js API Route for Streaming Responses (TypeScript) Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt A server-side Next.js API route that handles POST requests to stream responses using the Langbase SDK. It parses incoming JSON messages, initializes the Langbase client, calls the `pipes.run` method with streaming enabled, and returns the stream with appropriate headers. It requires the LANGBASE_API_KEY environment variable and uses the 'edge' runtime. ```typescript import { Langbase } from 'langbase'; import { NextRequest } from 'next/server'; export const runtime = 'edge'; export async function POST(req: NextRequest) { try { const { messages, threadId } = await req.json(); const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); const { stream, threadId: newThreadId } = await langbase.pipes.run({ name: 'chat-assistant', stream: true, threadId, messages, }); // Return stream with thread ID header return new Response(stream, { headers: { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'X-Thread-ID': newThreadId || '', }, }); } catch (error) { console.error('API Error:', error); return new Response( JSON.stringify({ error: error.message }), { status: 500, headers: { 'Content-Type': 'application/json' }, } ); } } ``` -------------------------------- ### Web Search with Exa Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Performs a web search using the Exa semantic search API. It allows specifying a query, the total number of desired results, and filtering by domains. Requires Langbase API key and Exa API key. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { const results = await langbase.tools.webSearch({ service: 'exa', query: 'Latest developments in transformer models 2024', totalResults: 5, domains: ['arxiv.org', 'huggingface.co'], apiKey: process.env.EXA_API_KEY!, }); console.log(`Found ${results.length} results:\n`); results.forEach((result, i) => { console.log(`${i + 1}. ${result.url}`); console.log(` ${result.content.substring(0, 200)}...`); console.log(); }); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Structured Output with JSON Schema Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Configure pipes to return structured JSON responses validated against a provided JSON schema. This ensures data consistency and simplifies post-processing of LLM outputs. ```APIDOC ## POST /pipes ### Description Creates a new pipe with specified configurations, including response format using JSON Schema for structured output. ### Method POST ### Endpoint /pipes ### Parameters #### Request Body - **name** (string) - Required - The name of the pipe. - **model** (string) - Optional - The language model to use (e.g., 'openai:gpt-4o'). - **json** (boolean) - Optional - If true, enables JSON mode for the response. - **response_format** (object) - Optional - Defines the format of the response. - **type** (string) - Required - The type of response format (e.g., 'json_schema'). - **json_schema** (object) - Required if type is 'json_schema'. Defines the JSON schema for the response. - **name** (string) - Required - The name of the JSON schema. - **schema** (object) - Required - The JSON schema object itself. - **strict** (boolean) - Optional - Enforces strict adherence to the schema. ### Request Example ```json { "name": "math-tutor", "model": "openai:gpt-4o", "json": true, "response_format": { "type": "json_schema", "json_schema": { "name": "math_reasoning", "schema": { "type": "object", "properties": { "steps": { "type": "array", "items": { "type": "object", "properties": { "explanation": {"type": "string"}, "output": {"type": "string"} }, "required": ["explanation", "output"] } }, "final_answer": {"type": "string"} }, "required": ["steps", "final_answer"] }, "strict": true } } } ``` ### Response #### Success Response (200) - **pipe** (object) - Details of the created pipe. - **name** (string) - The name of the pipe. - **apiKey** (string) - The API key associated with the pipe. #### Response Example ```json { "pipe": { "name": "math-tutor", "apiKey": "pipe_xyz789" } } ``` ``` -------------------------------- ### Create and Manage Conversation Threads Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Demonstrates explicit management of conversation threads, including creation with initial messages and metadata, appending new messages, retrieving the full thread, and updating thread metadata. Requires Langbase API key. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { // Create thread with initial messages const thread = await langbase.threads.create({ messages: [ { role: 'user', content: 'Hello! I need help with my account.', metadata: { priority: 'high' }, }, { role: 'assistant', content: "Hi! I'd be happy to help. What issue are you experiencing?", metadata: { sentiment: 'positive' }, }, ], metadata: { customer_id: 'cust_12345', department: 'support', created_at: new Date().toISOString(), }, }); console.log('Thread created:', thread.id); // Append messages await langbase.threads.append({ threadId: thread.id, messages: [ { role: 'user', content: 'I cannot access my dashboard.', }, ], }); // Get thread with all messages const fullThread = await langbase.threads.get({ threadId: thread.id, }); console.log('\nThread messages:'); fullThread.messages.forEach((msg) => { console.log(`${msg.role}: ${msg.content}`); }); // Update metadata await langbase.threads.update({ threadId: thread.id, metadata: { ...fullThread.metadata, status: 'resolved', resolved_at: new Date().toISOString(), }, }); console.log('\nThread updated'); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Upload Documents to Memory Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Uploads PDF and text documents to a specified memory with metadata. Requires the Langbase SDK and valid API key. Handles PDF and plain text content types. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; import fs from 'fs'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { // Upload PDF document const pdfResponse = await langbase.memories.documents.upload({ memoryName: 'company-docs', documentName: 'employee-handbook.pdf', document: fs.readFileSync('./employee-handbook.pdf'), contentType: 'application/pdf', meta: { category: 'hr', department: 'human-resources', version: '2024', }, }); console.log('PDF uploaded:', pdfResponse.name); // Upload text document const txtResponse = await langbase.memories.documents.upload({ memoryName: 'company-docs', documentName: 'onboarding-guide.txt', document: fs.readFileSync('./onboarding-guide.txt'), contentType: 'text/plain', meta: { category: 'onboarding', department: 'hr', }, }); console.log('Text file uploaded:', txtResponse.name); // List all documents const documents = await langbase.memories.documents.list({ memoryName: 'company-docs', }); console.log('\nAll documents:'); documents.forEach((doc) => { console.log(`- ${doc.name} (${doc.status})`); }); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Generate Text with Pipe (Non-Streaming) (TypeScript) Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Executes a pipe named 'summary' without streaming, sending a user prompt and printing the completion, thread ID, model, and token usage. Includes error handling for failed requests. Useful for simple one‑off text generation. ```TypeScript import 'dotenv/config'; import { Langbase } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY! }); async function main() { try { const response = await langbase.pipes.run({ name: 'summary', stream: false, messages: [ { role: 'user', content: 'Explain what an AI Engineer does in 2 sentences.' } ] }); console.log('Completion:', response.completion); console.log('Thread ID:', response.threadId); console.log('Model:', response.model); console.log('Tokens used:', response.usage.total_tokens); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Parse Documents with Langbase SDK Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Parses various document formats like PDF, Markdown, and CSV into plain text using the Langbase SDK. Requires API key and document files. Outputs document name and parsed content. ```typescript import 'dotenv/config'; import { Langbase } from 'langbase'; import fs from 'fs'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY!, }); async function main() { try { // Parse PDF const pdfResult = await langbase.parser({ document: fs.readFileSync('./research-paper.pdf'), documentName: 'research-paper.pdf', contentType: 'application/pdf', }); console.log('PDF parsed:'); console.log('Document:', pdfResult.documentName); console.log('Content length:', pdfResult.content.length, 'characters'); console.log('Preview:', pdfResult.content.substring(0, 200) + '...\n'); // Parse Markdown const mdResult = await langbase.parser({ document: fs.readFileSync('./README.md'), documentName: 'README.md', contentType: 'text/markdown', }); console.log('Markdown parsed:'); console.log('Document:', mdResult.documentName); console.log('Content length:', mdResult.content.length, 'characters'); // Parse CSV const csvResult = await langbase.parser({ document: fs.readFileSync('./data.csv'), documentName: 'data.csv', contentType: 'text/csv', }); console.log('\nCSV parsed:'); console.log('First 300 characters:', csvResult.content.substring(0, 300)); } catch (error) { console.error('Error:', error.message); } } main(); ``` -------------------------------- ### Stream Text with Pipe (Streaming) (TypeScript) Source: https://context7.com/langbaseinc/langbase-sdk/llms.txt Runs a pipe with streaming enabled, obtaining a stream and thread ID, then uses getRunner to handle real‑time content events, connection lifecycle, and errors. Ideal for interactive applications that require progressive output. ```TypeScript import 'dotenv/config'; import { Langbase, getRunner } from 'langbase'; const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY! }); async function main() { try { const { stream, threadId } = await langbase.pipes.run({ name: 'summary', stream: true, messages: [ { role: 'user', content: 'Write a short story about an AI learning to code.' } ] }); console.log('Thread ID:', threadId); const runner = getRunner(stream); runner.on('connect', () => { console.log('\\nStreaming started...\\n'); }); runner.on('content', (content) => { process.stdout.write(content); }); runner.on('end', () => { console.log('\\n\\nStream ended.'); }); runner.on('error', (error) => { console.error('\\nError:', error.message); }); } catch (error) { console.error('Error:', error.message); } } main(); ```