deep-chat
repository·main·Indexed 25 days ago
https://github.com/ovidijusparsiunas/deep-chatA highly customizable AI chat component (version 2.5.0) designed for easy integration into websites. It supports direct connections to major AI APIs, custom backend services, and browser-hosted models. The library includes example server templates for Phoenix, Go, Spring Boot, NextJS (App and Pages Router), Express, NestJS, Node.js WebSockets, and Flask to act as proxies for services like OpenAI, HuggingFace, StabilityAI, and Cohere.
What's inside deep-chat
- Deep Chat is a framework-agnostic web component designed to integrate AI services into any website. It is built to be highly customizable, allowing developers to modify both interactive features and styling details to meet unique API requirements and UX demands. It is shipped as a plug-and-play package compatible with various web frameworks.
Setup the DeepChat Phoenix Example Server
mainTo run the Phoenix LiveView template example for Deep Chat, follow these steps to install dependencies and start the server:
- Install Elixir dependencies:
mix setup - Install JavaScript dependencies (run this inside the
assetsfolder):npm install - Start the Phoenix server:
mix phx.server(oriex -S mix phx.serverto run within IEx)
Once started, the server is accessible at
http://localhost:4000.mix setup cd assets && npm install mix phx.server- Install Elixir dependencies:
Implement Server-Sent Events (SSE) for Deep Chat in Phoenix
mainTo support Server-Sent Events (SSE) in a Phoenix application for Deep Chat communication, implement the following:
- Use
Plug.Conn.send_chunkedandPlug.Conn.chunkto handle the stream. - Configure the MIME type in
config.exs:config :mime, :types, %{"text/event-stream" => ["sse"]} - Ensure your plug accepts the
ssetype:plug :accepts, ["json", "sse"]. - Nginx Note: If deploying behind Nginx, you must disable buffering by adding the header
put_resp_header("x-accel-buffering", "no")to your response.
- Use
Connect to Alibaba Cloud Qwen via directConnection
mainUse the
qwenproperty withindirectConnectionto connect directly to Alibaba Cloud's Qwen API. You can provide a booleantruefor default settings or an object to configure specific model parameters.Supported models include Qwen LMs, Qwen-VL, Qwen-Coder, Qwen-Omni, and Qwen-Math.
Configuration Options:
key: Your Alibaba Cloud API key.model: The name of the Qwen model (default:qwen-plus).temperature: Controls randomness (0.0-2.0).max_tokens: Maximum number of tokens to generate.top_p: Nucleus sampling diversity (0.0-1.0).frequency_penalty: Reduces repetition (-2.0 to 2.0).presence_penalty: Controls token repetition (-2.0 to 2.0).stop: Sequences where generation stops.system_prompt: Defines the model's role/objective.tools: Array ofQwenTooldeclarations.tool_choice: Controls tool usage ("auto" | "none" | {type: "function", function: {name: string}}).function_handler: Callback for handling tool calls.
<deep-chat directConnection='{ "qwen": { "key": "placeholder key", "system_prompt": "You are a helpful assistant.", "temperature": 0.7 } }' ></deep-chat>Implement Tool Calling with GeminiTool and FunctionHandler
mainGemini supports function calling. To implement this, you must provide two things in the
geminiconfiguration:tools(GeminiTool): An array of objects containingfunctionDeclarations. Each declaration includes aname,description, andparameters(defined using JSON Schema).function_handler(FunctionHandler): A function that receivesfunctionsDetails(information about which tools to call) and returns either:- An array of objects
{ response: string }[]containing the results for each tool call to be fed back to the model. - An object
{ text: string }to immediately display text in the chat.
- An array of objects
Example Implementation:
chatElementRef.directConnection = { gemini: { tools: [ { functionDeclarations: [ { 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']}, }, required: ['location'], }, }, ], }, ], function_handler: (functionsDetails) => { return functionsDetails.map((functionDetails) => { return { response: getCurrentWeather(functionDetails.arguments), }; }); }, key: 'placeholder-key', }, };Use the Deep Chat Playground
mainYou can create, configure, and test Deep Chat components visually without writing any code using the official Deep Chat Playground. This is useful for prototyping configurations before implementing them in your codebase.Use a custom URL for Ollama
mainIf your Ollama instance is running on a different endpoint than the default
http://localhost:11434/api/chat, use theconnectproperty to specify theurl.<deep-chat directConnection='{"ollama": true}' connect='{"url": "http://localhost:11434/api/chat"}' ></deep-chat>Set up the Deep Chat NextJS App Router template locally
mainThis template provides a NextJS App Router setup to communicate with the Deep Chat component. It includes endpoints that act as proxies for AI APIs like OpenAI, HuggingFace, StabilityAI, and Cohere.
To set it up locally:
- Clone the repository (shallow clone recommended to reduce size).
- Install dependencies using
npm install. - Start the development server with
npm run dev.
To use the proxy functions (e.g., OpenAI), you must provide the necessary API keys via environment variables.
git clone --depth 1 https://github.com/OvidijusParsiunas/deep-chat.git cd deep-chat/example-servers/nextjs/app-router npm install npm run devConnect to Open WebUI via directConnection
mainYou can connect the
<deep-chat>component directly to an Open WebUI instance using theopenWebUIproperty withindirectConnection. By default, it attempts to connect tohttp://localhost:3000/api/chat/completions.To connect to a remote instance, use the
connectproperty to specify a customurlpointing to your Open WebUI Chat Completions API endpoint.<deep-chat directConnection='{"openWebUI": {"key": "placeholder key", "model": "llama3.2:latest"}}' connect='{"url": "https://your-openwebui-instance.com/api/chat/completions"}' ></deep-chat>Implement Deep Chat in Vanilla JS
mainIn Vanilla JavaScript, you can import the component via a CDN. It is recommended to set values via properties, but using attributes is a valid fallback approach for other frameworks.Set up local example servers
mainThe
example-serversdirectory provides code examples for setting up custom backend servers to connect with the Deep Chat component. To download the project efficiently, use a shallow clone to reduce the download size.git clone --depth 1 https://github.com/OvidijusParsiunas/deep-chat.gitRun a chat model in the browser using Web Model
mainThe
webModelfeature allows you to run a chat model entirely within the user's browser without connecting to any external services. This provides privacy and reduces server costs.Setup
- Integrate the
deep-chat-web-llmmodule into your project. - Configure the
webModelproperty on the<deep-chat>component.
Configuration Options
You can set
webModeltotruefor default settings or provide an object to customize the behavior:model(string): The name of the model to be used. (Default:"Llama-3.2-1B-Instruct-q4f16_1-MLC")instruction(string): Directs how the model should respond.urls(WebModelUrls): Defines the endpoints to retrieve the web model assets.load(WebModelLoad): Defines how and when the model is loaded.introMessage(WebModelIntro): Configuration for the introductory web model message.worker(Worker): A Web Worker that can be used to enhance rendering performance.
<deep-chat webModel="true"></deep-chat>- Integrate the