An open API service indexing awesome lists of open source software.

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.

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;
```