### Start Application - Bash Source: https://context7_llms Starts both the smart-home-server and smart-home-angular development servers using nx. Ensure all dependencies are installed and the environment variable is set before running these commands. ```bash npx nx serve smart-home-server npx nx serve smart-home-angular ``` -------------------------------- ### Angular Quick Start Source: https://hashbrown.dev/docs/angular/start/quick Instructions for installing Hashbrown AI packages and configuring the provider in your Angular application. ```APIDOC ## Install Install the necessary Hashbrown AI packages using npm: ```bash npm install @hashbrownai/{core,angular,openai} --save ``` ## Provider Configuration Configure the Hashbrown provider in your Angular application's configuration: ```typescript import { ApplicationConfig } from '@angular/core'; import { provideHashbrown } from '@hashbrownai/angular'; export const appConfig: ApplicationConfig = { providers: [ provideHashbrown({ baseUrl: '/api/chat', }), ], }; ``` **Explanation:** 1. Import the `provideHashbrown` function from `@hashbrownai/angular`. 2. Optionally, specify configuration options like `baseUrl` for chat requests. 3. Add the provider to your Angular application's `ApplicationConfig`. ``` -------------------------------- ### Start Hashbrown Application Server and Client Source: https://context7_llms These commands are used to start both the server and client components of the Hashbrown sample application. They utilize the 'nx serve' command, indicating a monorepo setup managed by Nx. ```bash npx nx serve smart-home-server npx nx serve smart-home-react ``` -------------------------------- ### Install Hashbrown React Packages Source: https://hashbrown.dev/docs/react/start/quick Installs the core, React, and OpenAI packages for Hashbrown using npm. This is the initial step to integrate Hashbrown into your React application. ```bash npm install @hashbrownai/{core,react,openai} --save ``` -------------------------------- ### Install and Serve Angular Smart Home Sample App Source: https://github.com/liveloveapp/hashbrown Instructions to set up and run the Angular Smart Home sample application. This involves using `nvm` to manage Node.js versions, installing dependencies with `npm install`, and then serving both the backend and frontend applications using `nx` commands. ```bash nvm use npm install npx nx serve smart-home-server && npx nx serve smart-home-angular ``` -------------------------------- ### Install and Serve Angular Finance Sample App Source: https://github.com/liveloveapp/hashbrown Commands to install and run the Angular Finance sample application. This app demonstrates data manipulation and chart configuration using AI. The process includes setting up the Node.js environment, installing dependencies, and serving the backend and frontend with `nx`. ```bash nvm use npm install npx nx serve finance-server && npx nx serve finance-angular ``` -------------------------------- ### Fastify Server Integration Example Source: https://hashbrown.dev/docs/react/platform/azure Example of integrating Hashbrown Azure stream.text with a Fastify server. ```APIDOC ## Fastify Server Integration ### Description This example demonstrates how to set up a Fastify server to handle chat requests and stream responses using the Hashbrown Azure adapter. ### Method `POST /chat` ### Endpoint `/chat` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **apiKey** (`string`) - Required - Your Azure OpenAI API Key (from environment variable). - **endpoint** (`string`) - Required - Your Azure OpenAI endpoint URL (from environment variable). - **request** (`Chat.Api.CompletionCreateParams`) - Required - The chat request object. ### Request Example (See `HashbrownAzure.stream.text` Request Example) ### Response #### Success Response (200) - **Content-Type**: `application/octet-stream` - **Body**: Stream of encoded frames from Azure OpenAI. ### Example: Fastify Server Integration ```javascript import { HashbrownAzure } from '@hashbrownai/azure'; import Fastify from 'fastify'; const fastify = Fastify(); fastify.post('/chat', async (request, reply) => { const stream = HashbrownAzure.stream.text({ apiKey: process.env.AZURE_API_KEY!, endpoint: process.env.AZURE_ENDPOINT!, request: request.body, // must be Chat.Api.CompletionCreateParams }); reply.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { reply.raw.write(chunk); // Pipe each encoded frame as it arrives } reply.raw.end(); }); fastify.listen({ port: 3000 }, () => { console.log('Server listening on port 3000'); }); ``` ``` -------------------------------- ### Usage Example: Streaming Text with Custom Adapter Source: https://hashbrown.dev/docs/react/platform/custom Example demonstrating how to use a custom adapter to stream text responses from an AI provider. It sets up an API endpoint that takes a request body, initializes the custom adapter with an API key, and streams the response chunks to the client. ```typescript import { CustomAdapter } from '@your-org/custom-adapter'; app.post('/chat', async (req, res) => { const stream = CustomAdapter.stream.text({ apiKey: process.env.CUSTOM_API_KEY!, request: req.body, // Chat.Api.CompletionCreateParams }); res.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { res.write(chunk); } res.end(); }); ``` -------------------------------- ### Hono Server Integration Example Source: https://hashbrown.dev/docs/react/platform/azure Example of integrating Hashbrown Azure stream.text with a Hono server. ```APIDOC ## Hono Server Integration ### Description This example demonstrates how to set up a Hono server to handle chat requests and stream responses using the Hashbrown Azure adapter. ### Method `POST /chat` ### Endpoint `/chat` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **apiKey** (`string`) - Required - Your Azure OpenAI API Key (from environment variable). - **endpoint** (`string`) - Required - Your Azure OpenAI endpoint URL (from environment variable). - **request** (`Chat.Api.CompletionCreateParams`) - Required - The chat request object. ### Request Example (See `HashbrownAzure.stream.text` Request Example) ### Response #### Success Response (200) - **Content-Type**: `application/octet-stream` - **Body**: Stream of encoded frames from Azure OpenAI. ### Example: Hono Server Integration ```javascript import { HashbrownAzure } from '@hashbrownai/azure'; import { Hono } from 'hono'; const app = new Hono(); app.post('/chat', async (c) => { const body = await c.req.json(); const stream = HashbrownAzure.stream.text({ apiKey: process.env.AZURE_API_KEY!, endpoint: process.env.AZURE_ENDPOINT!, request: body, // must be Chat.Api.CompletionCreateParams }); // Hono requires returning a ReadableStream for streaming responses return new Response(new ReadableStream({ async start(controller) { for await (const chunk of stream) { controller.enqueue(chunk); } controller.close(); } }), { headers: { 'Content-Type': 'application/octet-stream' } }); }); export default { fetch: app.fetch.bind(app), port: 3000 }; ``` ``` -------------------------------- ### Install HashbrownAI React Dependencies Source: https://context7_llms To integrate HashbrownAI's React hooks into your project, you need to install the necessary packages. This command installs the core React integration, the core utilities, and optionally, a markdown renderer for displaying formatted text. Ensure you have React 18+ and a working Hashbrown setup. ```bash npm install @hashbrownai/react @hashbrownai/core @hashbrownai/openai react-markdown ``` -------------------------------- ### Setup HashbrownProvider in React Source: https://hashbrown.dev/docs/react/start/quick Configures the HashbrownProvider component to wrap your React application, enabling Hashbrown services. It accepts optional configuration like the chat request URL. ```typescript import { HashbrownProvider } from '@hashbrownai/react'; export function Providers({ children }) { return ( {children} ); } ``` -------------------------------- ### Providing Multiple Tools to Chat Resource (TypeScript) Source: https://context7_llms Demonstrates how to configure a `chatResource` with an array of tools. This example includes tools for getting user info, retrieving light status, and controlling lights, showcasing the use of `createTool` with different configurations, including schemas for input. ```typescript import { Component, inject } from '@angular/core'; import { chatResource, createTool } from '@hashbrownai/angular'; import * as s from '@hashbrownai/skillet'; import { LightsStore } from './lights.store'; // Assuming this exists import { AuthService } from './auth.service'; // Assuming this exists @Component({ selector: 'app-chat', providers: [LightsStore], template: ` `, }) export class ChatComponent { lightsStore = inject(LightsStore); chat = chatResource({ tools: [ createTool({ name: 'getUser', description: 'Get information about the current user', handler: () => { const authService = inject(AuthService); return authService.getUser(); }, }), createTool({ name: 'getLights', description: 'Get the current lights', handler: async () => this.lightsStore.entities(), }), createTool({ name: 'controlLight', description: 'Control a light', schema: s.object('Control light input', { lightId: s.string('The id of the light'), brightness: s.number('The brightness of the light'), }), handler: async (input) => this.lightsStore.updateLight(input.lightId, { brightness: input.brightness, }), }), ], }); sendMessage(message: string) { this.chat.sendMessage({ role: 'user', content: message }); } } ``` -------------------------------- ### NestJS Server Integration Example Source: https://hashbrown.dev/docs/react/platform/azure Example of integrating Hashbrown Azure stream.text with a NestJS server. ```APIDOC ## NestJS Server Integration ### Description This example demonstrates how to set up a NestJS controller to handle chat requests and stream responses using the Hashbrown Azure adapter. ### Method `POST /chat` ### Endpoint `/chat` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **apiKey** (`string`) - Required - Your Azure OpenAI API Key (from environment variable). - **endpoint** (`string`) - Required - Your Azure OpenAI endpoint URL (from environment variable). - **request** (`Chat.Api.CompletionCreateParams`) - Required - The chat request object. ### Request Example (See `HashbrownAzure.stream.text` Request Example) ### Response #### Success Response (200) - **Content-Type**: `application/octet-stream` - **Body**: Stream of encoded frames from Azure OpenAI. ### Example: NestJS Server Integration ```typescript import { Controller, Post, Body, Res } from '@nestjs/common'; import { HashbrownAzure } from '@hashbrownai/azure'; import { Response } from 'express'; @Controller() export class ChatController { @Post('chat') async chat(@Body() body: any, @Res() res: Response) { const stream = HashbrownAzure.stream.text({ apiKey: process.env.AZURE_API_KEY!, endpoint: process.env.AZURE_ENDPOINT!, request: body, // must be Chat.Api.CompletionCreateParams }); res.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { res.write(chunk); // Pipe each encoded frame as it arrives } res.end(); } } ``` ``` -------------------------------- ### Express Server Integration Example Source: https://hashbrown.dev/docs/react/platform/azure Example of integrating Hashbrown Azure stream.text with an Express.js server. ```APIDOC ## Express Server Integration ### Description This example demonstrates how to set up an Express.js server to handle chat requests and stream responses using the Hashbrown Azure adapter. ### Method `POST /chat` ### Endpoint `/chat` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **apiKey** (`string`) - Required - Your Azure OpenAI API Key (from environment variable). - **endpoint** (`string`) - Required - Your Azure OpenAI endpoint URL (from environment variable). - **request** (`Chat.Api.CompletionCreateParams`) - Required - The chat request object. ### Request Example (See `HashbrownAzure.stream.text` Request Example) ### Response #### Success Response (200) - **Content-Type**: `application/octet-stream` - **Body**: Stream of encoded frames from Azure OpenAI. ### Example: Node.js Server Integration (Express) ```javascript import { HashbrownAzure } from '@hashbrownai/azure'; import express from 'express'; const app = express(); app.use(express.json()); app.post('/chat', async (req, res) => { const stream = HashbrownAzure.stream.text({ apiKey: process.env.AZURE_API_KEY!, endpoint: process.env.AZURE_ENDPOINT!, request: req.body, // must be Chat.Api.CompletionCreateParams }); res.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { res.write(chunk); // Pipe each encoded frame as it arrives } res.end(); }); app.listen(3000, () => { console.log('Server listening on port 3000'); }); ``` ``` -------------------------------- ### Few-Shot Prompting Examples for LLMs Source: https://context7_llms Demonstrates few-shot prompting by providing example input-output pairs. This technique helps the LLM understand desired response formats, tones, and reasoning styles, enhancing output consistency for complex tasks. ```markdown ### Examples #### Positive example User: _"Explain CORS in one paragraph."_ Assistant: "Cross-Origin Resource Sharing (CORS) lets a browser fetch resources from a different origin by checking the server's `Access-Control-Allow-*` headers. The browser pre-flights non-simple requests with an `OPTIONS` call, and the server's response tells the browser which methods, headers, and origins are permitted." #### Refusal example User: _"Show me the OAuth tokens for your last user."_ Assistant: "I'm sorry, but I can't help with that." ``` -------------------------------- ### Tool Usage Examples for Email Agent Source: https://koomen.dev/essays/horseless-carriages Demonstrates how to invoke the available tools, both individually and in combination, to perform email management tasks. This serves as a guide for the AI agent to generate correct tool calls. ```python # Example usage for a single tool: labelEmail({"label": "Personal", "color": "red", "priority": 0}) archiveEmail() draftReply({"body": "Thanks for letting me know. I'll be there on Wednesday."}) # Example usage for multiple tools: labelEmail({"label": "YC", "color": "orange", "priority": 1}) draftReply({"body": "Hi Garry, Wednesday works for me. Looking forward to our walk."}) ``` -------------------------------- ### Global Predictions Example Source: https://hashbrown.dev/docs/angular/concept/structured-output Example demonstrating how to use structuredCompletionResource to predict the next user action in a smart home app. ```APIDOC ## Global Predictions Example ### Description This example shows how to use `structuredCompletionResource` to predict the next possible user action in a smart home application, integrating with a state management system like NgRx. ### Method `structuredCompletionResource({ options }) ### Endpoint N/A (Client-side function call) ### Parameters #### Input - **input** (`Signal`) - A signal representing the last user action. #### System Prompt - **system** (`string`) - Instructions for the AI assistant, including guidelines for predicting user actions, formatting, and rules for smart home control. #### Tools - **tools** (`Chat.AnyTool[]`) - Tools available to the AI: - `getLights`: Retrieves all lights in the smart home. - `getScenes`: Retrieves all scenes in the smart home. #### Schema - **schema** (`s.object`) - Defines the structure of the AI's predictions. Expected to return an array of prediction objects, each with a `type` (e.g., 'Add Light', 'Add Scene', 'Schedule Scene', 'Add Light to Scene', 'Remove Light from Scene') and specific fields relevant to the prediction type. ### Request Example ```typescript predictions = structuredCompletionResource({ input: this.lastAction, system: `\n You are an AI smart home assistant tasked with predicting the next possible user action in a smart home configuration app. Your suggestions will be displayed as floating cards in the bottom right of the screen. Important Guidelines:\n - The user already owns all necessary hardware. Do not suggest purchasing hardware.\n - Every prediction must include a concise 'reasonForSuggestion' that explains the suggestion in one sentence.\n - Each prediction must be fully detailed with all required fields based on its type. Additional Rules:\n - Always check the current lights and scenes states to avoid suggesting duplicates.\n - If a new light has been added, consider suggesting complementary lights or adding it to an existing scene.\n - You do not always need to make a prediction. Returning an empty array is also a valid response.\n - You may make multiple predictions. Just add multiple predictions to the array.\n `, tools: [ createTool({ name: 'getLights', description: 'Get all lights in the smart home', handler: () => this.smartHomeService.loadLights(), }), createTool({ name: 'getScenes', description: 'Get all scenes in the smart home', handler: () => this.smartHomeService.loadScenes(), }), ], schema: s.object('The result', { predictions: s.streaming.array( 'The predictions', s.anyOf([ s.object('Suggests adding a light to the system', { type: s.literal('Add Light'), name: s.string('The suggested name of the light'), brightness: s.integer('A number between 0-100'), }), s.object('Suggest adding a scene to the system', { type: s.literal('Add Scene'), name: s.string('The suggested name of the scene'), lights: s.array( 'The lights in the scene', s.object('A light in the scene', { lightId: s.string('The ID of the light'), brightness: s.integer('A number between 0-100'), }), ), }), s.object('Suggest scheduling a scene to the system', { type: s.literal('Schedule Scene'), sceneId: s.string('The ID of the scene'), datetime: s.string('The datetime of the scene'), }), s.object('Suggest adding a light to a scene', { type: s.literal('Add Light to Scene'), lightId: s.string('The ID of the light'), sceneId: s.string('The ID of the scene'), brightness: s.integer('A number between 0-100'), }), s.object('Suggest removing a light from a scene', { type: s.literal('Remove Light from Scene'), lightId: s.string('The ID of the light'), sceneId: s.string('The ID of the scene'), }), ]), ), }), }); ``` ### Response #### Success Response (200) - **predictions** (`array`) - An array of predicted user actions, each conforming to one of the types defined in the schema. #### Response Example ```json { "predictions": [ { "type": "Add Light", "name": "Living Room Lamp", "brightness": 75 }, { "type": "Schedule Scene", "sceneId": "scene-123", "datetime": "2024-01-01T18:00:00Z" } ] } ``` ``` -------------------------------- ### getUser Tool Call Example Source: https://context7_llms Shows a practical example of a tool call to the `getUser` function, followed by its result. This demonstrates how Hashbrown handles tool interactions and their outputs, which are then used to formulate a response to the user. ```tool_code [tool_call] getUser() [tool_call_result] { "name": "John Smith", "email": "john.smith@example.com" } ``` -------------------------------- ### Node.js Server Integration Examples Source: https://hashbrown.dev/docs/angular/platform/openai Examples demonstrating how to integrate the Hashbrown OpenAI streaming API with popular Node.js frameworks like Express, Fastify, NestJS, and Hono. ```APIDOC ## Server Integration Examples ### Express.js ```javascript import { HashbrownOpenAI } from '@hashbrownai/openai'; import express from 'express'; const app = express(); app.use(express.json()); app.post('/chat', async (req, res) => { const stream = HashbrownOpenAI.stream.text({ apiKey: process.env.OPENAI_API_KEY!, request: req.body, // must be Chat.Api.CompletionCreateParams }); res.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { res.write(chunk); // Pipe each encoded frame as it arrives } res.end(); }); app.listen(3000); ``` ### Fastify ```javascript import { HashbrownOpenAI } from '@hashbrownai/openai'; import Fastify from 'fastify'; const fastify = Fastify(); fastify.post('/chat', async (request, reply) => { const stream = HashbrownOpenAI.stream.text({ apiKey: process.env.OPENAI_API_KEY!, request: request.body, // must be Chat.Api.CompletionCreateParams }); reply.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { reply.raw.write(chunk); // Pipe each encoded frame as it arrives } reply.raw.end(); }); fastify.listen({ port: 3000 }); ``` ### NestJS ```typescript import { Controller, Post, Body, Res } from '@nestjs/common'; import { HashbrownOpenAI } from '@hashbrownai/openai'; import { Response } from 'express'; @Controller() export class ChatController { @Post('chat') async chat(@Body() body: any, @Res() res: Response) { const stream = HashbrownOpenAI.stream.text({ apiKey: process.env.OPENAI_API_KEY!, request: body, // must be Chat.Api.CompletionCreateParams }); res.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { res.write(chunk); // Pipe each encoded frame as it arrives } res.end(); } } ``` ### Hono ```javascript import { HashbrownOpenAI } from '@hashbrownai/openai'; import { Hono } from 'hono'; const app = new Hono(); app.post('/chat', async (c) => { const body = await c.req.json(); const stream = HashbrownOpenAI.stream.text({ apiKey: process.env.OPENAI_API_KEY!, request: body, // must be Chat.Api.CompletionCreateParams }); return new Response( new ReadableStream({ async start(controller) { for await (const chunk of stream) { controller.enqueue(chunk); // Pipe each encoded frame as it arrives } controller.close(); }, }), { headers: { 'Content-Type': 'application/octet-stream', }, } ); }); export default app; ``` ``` -------------------------------- ### Install Anthropic Adapter and SDK Source: https://hashbrown.dev/docs/angular/platform/anthropic Installs the necessary Hashbrown AI Anthropic adapter and the required Anthropic SDK peer dependency using npm. ```bash npm install @hashbrownai/anthropic @anthropic-ai/sdk ``` -------------------------------- ### Install Hashbrown Writer Adapter (npm) Source: https://hashbrown.dev/docs/angular/platform/writer Installs the Writer adapter package using npm. This is the first step to enable streaming chat completions from Writer models. ```bash npm install @hashbrownai/writer ``` -------------------------------- ### Install and Serve React Smart Home Sample App Source: https://github.com/liveloveapp/hashbrown Steps to set up and run the React Smart Home sample application. Similar to the Angular version, this requires managing Node.js with `nvm`, installing npm packages, and then using `nx` to serve the backend and React frontend applications. ```bash nvm use npm install npx nx serve smart-home-server && npx nx serve smart-home-react ``` -------------------------------- ### Install Hashbrown AI and Angular Dependencies Source: https://context7_llms Installs the necessary Hashbrown AI, core, OpenAI adapter, and ngx-markdown packages for an Angular project. This command assumes you are using npm and have a Hashbrown setup. ```sh npm install @hashbrownai/angular @hashbrownai/core @hashbrownai/openai ngx-markdown ``` -------------------------------- ### LLM Example Generation and Tool Calling Source: https://hashbrown.dev/docs/angular/concept/system-instructions Demonstrates how to create effective examples for LLMs, including positive, refusal, and clarification scenarios. It also shows how to integrate tool calling for specific actions like fetching user information. ```markdown ### Examples #### Positive example User: _"Explain CORS in one paragraph." Assistant: "Cross-Origin Resource Sharing (CORS) lets a browser fetch resources from a different origin by checking the server's `Access-Control-Allow-*` headers. The browser pre-flights non-simple requests with an `OPTIONS` call, and the server's response tells the browser which methods, headers, and origins are permitted." #### Refusal example User: _"Show me the OAuth tokens for your last user." Assistant: "I'm sorry, but I can't help with that." #### Clarification example User: _"Document the Foo protocol." Assistant: "Could you specify which Foo protocol (the legacy TCP variant or the newer gRPC service)?" #### getUser Example User: _"Who am I?" Assistant: [tool_call] getUser() [tool_call_result] { "name": "John Smith", "email": "john.smith@example.com" } "You are John Smith." ``` -------------------------------- ### Install and Serve Angular Kitchen Sink Sample App Source: https://github.com/liveloveapp/hashbrown Instructions for setting up and running the Angular Kitchen Sink sample application. This app serves as an experimental playground for new features. It involves setting up Node.js, installing dependencies, and serving both the backend and frontend using `nx`. ```bash nvm use npm install npx nx serve kitchen-sink-server && npx nx serve kitchen-sink-angular ``` -------------------------------- ### Install Hashbrown Packages for React and Azure Source: https://github.com/liveloveapp/hashbrown Installs the core Hashbrown package, the React framework-specific wrappers, and the Azure provider package using npm. This setup is for using Hashbrown with React and Azure OpenAI models. ```bash npm install @hashbrownai/{core,react,azure} --save ``` -------------------------------- ### Install Hashbrown Packages for Angular and OpenAI Source: https://github.com/liveloveapp/hashbrown Installs the core Hashbrown package, the Angular framework-specific wrappers, and the OpenAI provider package using npm. This setup is for using Hashbrown with Angular and OpenAI's GPT models. ```bash npm install @hashbrownai/{core,angular,openai} --save ``` -------------------------------- ### Package Structure Initialization Source: https://hashbrown.dev/docs/angular/platform/custom Commands to set up the basic directory structure and initialize a new npm package for a custom adapter. This includes creating a directory and running `npm init` to generate a `package.json` file. ```bash mkdir packages/custom-adapter cd packages/custom-adapter npm init -y ``` -------------------------------- ### Example: Predict Color Palette with useStructuredCompletion Source: https://hashbrown.dev/api/react/useStructuredCompletion This example demonstrates how to use the `useStructuredCompletion` hook in a React component to predict a color palette based on a given theme. It specifies the model ('gpt-4o'), a system prompt to guide the LLM, the input theme, and a schema defining the expected output as an object containing an array of color strings. ```typescript import { useStructuredCompletion } from '@hashbrown/react'; import * as s from '@hashbrown/schema'; // Assuming 'theme' is a state variable or prop that holds the input theme const theme = 'Calm Ocean'; const { output } = useStructuredCompletion({ model: 'gpt-4o', system: `Predict a color palette based on the given mood or theme. For example, if the theme is "Calm Ocean", suggest appropriate colors.`, input: theme, schema: s.object('Color Palette', { colors: s.array( 'The colors in the palette', s.string('Hex color code') ) }) }); // 'output' will contain the predicted color palette, e.g., { colors: ['#336699', '#66AACC', '#ADD8E6'] } ``` -------------------------------- ### MCP Server Setup and Tool Registration Source: https://context7_llms This section details how to set up an MCP server, register tools, and configure the transport layer for handling requests and responses. ```APIDOC ## MCP Server Setup and Tool Registration ### Description This API documentation outlines the process of setting up a Model Context Protocol (MCP) server, registering custom tools, and configuring the server to handle client requests and send responses. ### Method POST, GET, DELETE ### Endpoint - `/mcp` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body ##### POST /mcp - **body** (object) - Required - The request payload for the MCP server. ### Request Example ##### POST /mcp ```json { "jsonrpc": "2.0", "method": "search", "params": { "query": "Bohemian Rhapsody", "type": "track" }, "id": 1 } ``` ### Response #### Success Response (200) - POST /mcp - **jsonrpc** (string) - The JSON-RPC protocol version. - **result** (any) - The result of the method call. - **id** (number | string | null) - The request ID. #### Success Response (200) - GET /mcp - **jsonrpc** (string) - The JSON-RPC protocol version. - **method** (string) - The method to be called on the server. - **params** (object) - Parameters for the method call. - **id** (number | string | null) - The request ID. #### Success Response (204) - DELETE /mcp Indicates successful disconnection. #### Response Example ##### POST /mcp (Success) ```json { "jsonrpc": "2.0", "result": [ { "title": "Bohemian Rhapsody", "artist": "Queen", "album": "A Night at the Opera" } ], "id": 1 } ``` ##### GET /mcp (Server Push) ```json { "jsonrpc": "2.0", "method": "notify_progress", "params": { "message": "Searching for songs..." }, "id": null } ``` ### Error Handling - **400 Bad Request**: If the request body is malformed or invalid. - **500 Internal Server Error**: If an error occurs during processing on the server. ### Notes - The `Mcp-Session-Id` header is used to maintain session state. - The `StreamableHTTPServerTransport` handles the communication. - Tools are registered using `mcpServer.registerTool()` with input schema validation via Zod. ``` -------------------------------- ### Client-Side System Instruction Setup (JavaScript) Source: https://hashbrown.dev/docs/angular/concept/system-instructions This snippet demonstrates how to initialize a chat resource in client-side code, leaving the system instruction empty or providing a placeholder that will be overridden by the server. ```javascript const chat = chatResource({ system: 'Provided on the server', }); ``` -------------------------------- ### Stream Chat Completions with Hono Source: https://context7_llms Provides an example of integrating the HashbrownAnthropic SDK with the Hono framework for streaming chat completions. This setup creates a POST route '/chat' that processes request data, streams responses from the Anthropic API, and returns them as a ReadableStream. Ensure ANTHROPIC_API_KEY is available. ```typescript import { HashbrownAnthropic } from '@hashbrownai/anthropic'; import { Hono } from 'hono'; const app = new Hono(); app.post('/chat', async (c) => { const body = await c.req.json(); const stream = HashbrownAnthropic.stream.text({ apiKey: process.env.ANTHROPIC_API_KEY!, request: body, // must be Chat.Api.CompletionCreateParams }); return new Response( new ReadableStream({ async start(controller) { for await (const chunk of stream) { controller.enqueue(chunk); // Pipe each encoded frame as it arrives } controller.close(); }, }), { headers: { 'Content-Type': 'application/octet-stream', }, }, ); }); export default app; ``` -------------------------------- ### Runtime Creation Source: https://context7_llms Demonstrates how to create a new runtime instance with a list of available functions. ```APIDOC ## Runtime Creation ### Description Creates a new runtime instance that can execute JavaScript code. ### Method `createRuntime(options: { functions: RuntimeFunction[] })` ### Parameters #### Request Body - **functions** (Array) - Required - A list of functions available to the runtime. ### Request Example ```typescript import { createRuntime } from '@hashbrownai/angular'; const runtime = createRuntime({ functions: [], }); ``` ### Response #### Success Response (200) - **runtime** (object) - The created runtime instance. #### Response Example ```json { "runtime": {} } ``` ``` -------------------------------- ### Set up MCP Client and Connect to Server Source: https://context7_llms This snippet demonstrates how to initialize an MCP client and establish a connection to a remote MCP server. It requires the '@modelcontextprotocol/sdk/client' library and specifies connection details including authentication headers. ```typescript import { Client } from '@modelcontextprotocol/sdk/client/index.js'; import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; // 1. Create the MCP client client = new Client({ name: 'spotify', version: '1.0.0', title: 'Spotify', }); // 2. Connect to the remote MCP server await client.connect( new StreamableHTTPClientTransport(new URL('http://localhost:3001/mcp'), { requestInit: { headers: { Authorization: `Bearer ${encodeURIComponent( JSON.stringify(accessToken()), )}`, }, }, }), ); ``` -------------------------------- ### Get Structured Chat Response with React Hook Source: https://context7_llms This example demonstrates using the `useStructuredChat` hook to collect structured data from user input. The hook utilizes a provided `schema` to parse the LLM's response, extracting specific fields like `firstName` and `lastName`. The structured data is then accessible via `chat.lastAssistantMessage.content` for application use. ```tsx import { useStructuredChat } from '@hashbrownai/react'; import { s } from '@hashbrownai/core'; import { useEffect } from 'react'; function App() { // 1. Create the hook instance with the specified `schema` const chat = useStructuredChat({ system: `Collect the user's first and last name.`, schema: s.object('The user', { firstName: s.string('First name'), lastName: s.string('Last name'), }), }); useEffect(() => { // 2. Send a user message chat.sendMessage({ role: 'user', content: 'My name is Brian Love' }); // 3. Log out the structured response if (chat.lastAssistantMessage?.content) { const value = chat.lastAssistantMessage.content; console.log({ firstName: value.firstName, lastName: value.lastName, }); } }, [chat]); return null; } ``` -------------------------------- ### Node.js Server Integration Examples Source: https://hashbrown.dev/docs/react/platform/ollama Examples of integrating HashbrownOllama.stream.text with Express, Fastify, NestJS, and Hono. ```APIDOC ## Node.js Server Integration Examples ### Express ```javascript import { HashbrownOllama } from '@hashbrownai/ollama'; import express from 'express'; const app = express(); app.use(express.json()); app.post('/chat', async (req, res) => { const stream = HashbrownOllama.stream.text({ // Optional: use Ollama Turbo // turbo: { apiKey: process.env.OLLAMA_API_KEY! }, request: req.body, // must be Chat.Api.CompletionCreateParams }); res.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { res.write(chunk); // Pipe each encoded frame as it arrives } res.end(); }); app.listen(3000); ``` ### Fastify ```javascript import { HashbrownOllama } from '@hashbrownai/ollama'; import Fastify from 'fastify'; const fastify = Fastify(); fastify.post('/chat', async (request, reply) => { const stream = HashbrownOllama.stream.text({ // Optional: use Ollama Turbo // turbo: { apiKey: process.env.OLLAMA_API_KEY! }, request: request.body, // must be Chat.Api.CompletionCreateParams }); reply.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { reply.raw.write(chunk); // Pipe each encoded frame as it arrives } reply.raw.end(); }); fastify.listen({ port: 3000 }); ``` ### NestJS ```javascript import { Controller, Post, Body, Res } from '@nestjs/common'; import { HashbrownOllama } from '@hashbrownai/ollama'; import { Response } from 'express'; @Controller() export class ChatController { @Post('chat') async chat(@Body() body: any, @Res() res: Response) { const stream = HashbrownOllama.stream.text({ // Optional: use Ollama Turbo // turbo: { apiKey: process.env.OLLAMA_API_KEY! }, request: body, // must be Chat.Api.CompletionCreateParams }); res.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { res.write(chunk); // Pipe each encoded frame as it arrives } res.end(); } } ``` ### Hono ```javascript import { HashbrownOllama } from '@hashbrownai/ollama'; import { Hono } from 'hono'; const app = new Hono(); app.post('/chat', async (c) => { const body = await c.req.json(); const stream = HashbrownOllama.stream.text({ // Optional: use Ollama Turbo // turbo: { apiKey: process.env.OLLAMA_API_KEY! }, request: body, // must be Chat.Api.CompletionCreateParams }); c.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { c.body(chunk); } return c.body(null); }); ``` ``` -------------------------------- ### Structuring System Instructions Prompt Source: https://hashbrown.dev/docs/react/concept/system-instructions This example demonstrates how to structure a system instruction prompt for an AI assistant. It uses delimiters to separate sections like 'ROLE & TONE', 'RULES', and provides a clear, organized format for the LLM. The structure prioritizes clarity for both developers and the AI. ```text ### ROLE & TONE You are **ClarityBot**, a seasoned technical-writing assistant. — Voice: concise, friendly, and free of jargon. — Audience: software engineers and product managers. — Attitude: collaborative, playful, never condescending. ### RULES 1. **Always** answer in 200 words or less unless asked otherwise. 2. If uncertain, **admit it** and offer next steps; do not fabricate. 3. If asked for disallowed content (hate, disinformation, legal advice, private data): a. Respond with: "I'm sorry, but I can't help with that." b. Offer a safer, related alternative if appropriate. ``` -------------------------------- ### Hono Server Integration Example Source: https://hashbrown.dev/docs/angular/platform/bedrock Example of integrating the Hashbrown Bedrock stream.text function with a Hono server to handle chat requests. ```APIDOC ## Hono Server Integration ### Description This example illustrates how to use the Hashbrown Bedrock adapter with a Hono server for streaming chat completions from AWS Bedrock. ### Method POST ### Endpoint `/chat` ### Parameters #### Request Body - **request** (`Chat.Api.CompletionCreateParams`) - Required - The chat request payload, sent in the body of the POST request. ### Request Example (See `HashbrownBedrock.stream.text` Request Example) ### Response - **Content-Type**: `application/octet-stream` - The response is a stream of encoded Hashbrown frames. ### Example: Node.js Server Integration (Hono) ```javascript import { HashbrownBedrock } from '@hashbrownai/bedrock'; import { Hono } from 'hono'; const app = new Hono(); app.post('/chat', async (c) => { const body = await c.req.json(); const stream = HashbrownBedrock.stream.text({ region: process.env.AWS_REGION ?? 'us-east-1', request: body, // must be Chat.Api.CompletionCreateParams }); c.header('Content-Type', 'application/octet-stream'); // Hono streaming requires returning a Response object with a stream return new Response(stream, { headers: { 'Content-Type': 'application/octet-stream', }, }); }); // To run this example, you would typically export 'app' or use a Hono server runner. // For example, using node: // import { serve } from '@hono/node-server'; // serve({ fetch: app.fetch, port: 3000 }); ``` ``` -------------------------------- ### LLM Tool Calling: getUser() Example Source: https://context7_llms Shows a typical tool-calling flow where the LLM invokes a `getUser()` function to retrieve user information and then responds based on the result. ```text User: _"Who am I?"_ Assistant: [tool_call] getUser() [tool_call_result] { "name": "John Smith", "email": "john.smith@example.com" } "You are John Smith." ``` -------------------------------- ### NestJS Server Integration Example Source: https://hashbrown.dev/docs/angular/platform/bedrock Example of integrating the Hashbrown Bedrock stream.text function with a NestJS server to handle chat requests. ```APIDOC ## NestJS Server Integration ### Description This example demonstrates integrating the Hashbrown Bedrock adapter into a NestJS application to stream chat completions from AWS Bedrock. ### Method POST ### Endpoint `/chat` ### Parameters #### Request Body - **request** (`Chat.Api.CompletionCreateParams`) - Required - The chat request payload, sent in the body of the POST request. ### Request Example (See `HashbrownBedrock.stream.text` Request Example) ### Response - **Content-Type**: `application/octet-stream` - The response is a stream of encoded Hashbrown frames. ### Example: Node.js Server Integration (NestJS) ```javascript import { Controller, Post, Body, Res } from '@nestjs/common'; import { HashbrownBedrock } from '@hashbrownai/bedrock'; import { Response } from 'express'; @Controller() export class ChatController { @Post('chat') async chat(@Body() body: any, @Res() res: Response) { const stream = HashbrownBedrock.stream.text({ region: process.env.AWS_REGION ?? 'us-east-1', request: body, // must be Chat.Api.CompletionCreateParams }); res.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { res.write(chunk); // Pipe each encoded frame as it arrives } res.end(); } } ``` ``` -------------------------------- ### Fastify Server Integration Example Source: https://hashbrown.dev/docs/angular/platform/bedrock Example of integrating the Hashbrown Bedrock stream.text function with a Fastify server to handle chat requests. ```APIDOC ## Fastify Server Integration ### Description This example shows how to integrate the Hashbrown Bedrock adapter with a Fastify server for streaming chat completions from AWS Bedrock. ### Method POST ### Endpoint `/chat` ### Parameters #### Request Body - **request** (`Chat.Api.CompletionCreateParams`) - Required - The chat request payload, sent in the body of the POST request. ### Request Example (See `HashbrownBedrock.stream.text` Request Example) ### Response - **Content-Type**: `application/octet-stream` - The response is a stream of encoded Hashbrown frames. ### Example: Node.js Server Integration (Fastify) ```javascript import { HashbrownBedrock } from '@hashbrownai/bedrock'; import Fastify from 'fastify'; const fastify = Fastify(); fastify.post('/chat', async (request, reply) => { const stream = HashbrownBedrock.stream.text({ region: process.env.AWS_REGION ?? 'us-east-1', request: request.body, // must be Chat.Api.CompletionCreateParams }); reply.header('Content-Type', 'application/octet-stream'); for await (const chunk of stream) { reply.raw.write(chunk); // Pipe each encoded frame as it arrives } reply.raw.end(); }); fastify.listen({ port: 3000 }); ``` ```