https://github.com/levivoelz/langchain-websocket-typescript
A simple node websocket server example that uses LangChain and Ollama to generate responses.
https://github.com/levivoelz/langchain-websocket-typescript
express langchain ollama typescript websockets
Last synced: 2 months ago
JSON representation
A simple node websocket server example that uses LangChain and Ollama to generate responses.
- Host: GitHub
- URL: https://github.com/levivoelz/langchain-websocket-typescript
- Owner: levivoelz
- License: mit
- Created: 2024-09-16T04:39:50.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-16T04:40:44.000Z (about 1 year ago)
- Last Synced: 2025-07-22T01:56:52.316Z (3 months ago)
- Topics: express, langchain, ollama, typescript, websockets
- Language: TypeScript
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Langchain websocket server example with TypeScript and Ollama
This is a simple websocket server that uses LangChain and Ollama to provide a chat interface.
## Run Demo
Uses Node.js v22.6.0 or later.
Install Ollama or use another LLM compatible with LangChain. Run `ollama run llama3.1` to start the Ollama server.
```bash
npm install
npm start
```In a separate terminal, run the test to see the completion response:
```bash
npm test
```## How to create a new websocket client to connect to the server.
_Examples were generated with an LLM and were not tested._Vanilla example:
```typescript
const ws = new WebSocket("ws://localhost:8080");ws.on("open", () => {
console.log("Connected to the server");
});ws.on("message", (data) => {
console.log("Received message:", data);
});ws.on("close", () => {
console.log("Disconnected from the server");
});ws.send("Hello");
```Example with React:
```typescript
import { useEffect, useState } from "react";const App = () => {
const [messages, setMessages] = useState([]);
const [input, setInput] = useState("");useEffect(() => {
const ws = new WebSocket('ws://localhost:3000');ws.onopen = () => {
console.log('WebSocket connection established.');
};ws.onmessage = (event) => {
setMessages((prevMessages) => [...prevMessages, event.data]);
};return () => {
ws.close();
};
}, []);return (
WebSocket Chat
{messages.map((message, index) => (
{message}
))}
setInput(e.target.value)} />
Send
);
};export default App;
```