Zack Saadioui
8/27/2024
1
2
bash
ollama pull <MODEL_NAME>
1
[Llama2-7B](https://ollama.ai/library/llama2)
1
[Mistral-7B](https://ollama.ai/library/mistral)
1
2
bash
ollama serve
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class OllamaSingleton { private static instance: OllamaSingleton | null = null; private modelName: string; private constructor(modelName: string) { this.modelName = modelName; // Initialization code for the model can go here } public static getInstance(modelName: string): OllamaSingleton { if (this.instance === null) { this.instance = new OllamaSingleton(modelName); } return this.instance; } }
1
getInstance
1
2
typescript
const ollamaModel = OllamaSingleton.getInstance('mistral'); // or any model name
1
OllamaSingleton
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
class OllamaSingleton { private static instance: OllamaSingleton | null = null; private modelName: string; private constructor(modelName: string) { this.modelName = modelName; // Initialization code for the model can go here } public static getInstance(modelName: string): OllamaSingleton { if (this.instance === null) { this.instance = new OllamaSingleton(modelName); } return this.instance; } public async sendPrompt(prompt: string): Promise<string> { // Logic to interact with the Ollama model using REST API const response = await fetch(`http://localhost:11434/api/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ model: this.modelName, prompt }) }); const data = await response.json(); return data.result; } }
1
sendPrompt
1
2
3
4
typescript
const chatBotModel = OllamaSingleton.getInstance('mistral');
const userResponse = await chatBotModel.sendPrompt('Tell me about the weather today.');
console.log(userResponse);
Copyright © Arsturn 2024