Local AI API Setup
2025-03-042 turns4,465 charsgpt-4o
Summary
User wants to connect their HTML-based AI chat project to a locally hosted Ollama llama2 instance via API.
Messages
How to setup local Hosted AI API for coded project?
Question
I have coded a project (AI Chat) in html and I installed Ollama llama2 locally. I want to request the AI with API on my coded project, Could you please help me how to do that? I found nothing on Youtube for this certain case Thank you
Upvote
1
Downvote
6
Go to comments
Share
Share
Join the conversation
u/monday_com avatar
monday_com
•
Promoted
Your team deserves monday.com, a platform that helps your team work more efficiently, productively, and without confusion. Try it now!
Sign Up
monday.com
Thumbnail image: Your team deserves monday.com, a platform that helps your team work more efficiently, productively, and without confusion. Try it now!
Sort by:
Best
Search Comments
Expand comment search
Comments Section
polandtown
•
1h ago
My dearest redditor, I searched on Youtube, "how to call your llm to your website" and there's thousands if not tens of thousands of results. Here's an example. I hope the ~12+ other subs you posted to are gentle in their responses.
Here's the second result - https://www.youtube.com/watch?v=syR0fT0rkgY
This is going to make me sound like an ass, but do your coding career a favor and also look up how to ask coding questions online. There's an etiquette, and it will save you a ton of time sifting though responses. Best of luck with your project and take care!
Upvote
2
Downvote
Reply
reply
Award
Share
Share
Shot-Negotiation5968
OP
•
49m ago
Thank you very much, I have to be honest that I am so new to all that, as you probably noticed while you red my post, that I didn t even know what to searcj for exactly to get the case I have.
Upvote
2
Downvote
Reply
reply
Award
Share
Share
mp3m4k3r
•
1h ago
Do you mean having your web app call the OpenAI compatible endpoint hosted by Ollama?
Upvote
1
Downvote
Reply
reply
Award
Share
Share
Shot-Negotiation5968
OP
•
57m ago
Yes exactly, I am new to this and this is my first project. I have an HTML css js code of the chat interface, and I have installed llama2 locally but I am completely lost how to continue
No worries! You’re on the right track, and I’ll guide you through it step by step. You want your HTML, CSS, and JavaScript chat interface to send requests to your locally hosted **Ollama** LLM (Llama 2). Here’s how you can do it.
---
### **1. Run Ollama API Locally**
Ollama provides an API that listens on `http://localhost:11434`. First, make sure Ollama is running by opening a terminal and typing:
```sh
ollama run llama2
```
or, if you want it to run in the background:
```sh
ollama serve
```
This ensures that your Llama 2 model is available via an API.
---
### **2. Send Requests to Ollama from Your Web App**
Now, modify your JavaScript code to send a request from your frontend (HTML + JavaScript) to your local Ollama API.
Here’s how you can do it using **Fetch API** in JavaScript:
```js
async function sendMessage(userMessage) {
const response = await fetch("http://localhost:11434/api/generate", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "llama2",
prompt: userMessage,
stream: false
})
});
const data = await response.json();
return data.response; // Get the model's reply
}
// Example usage with an input field and button
document.getElementById("sendBtn").addEventListener("click", async () => {
const userInput = document.getElementById("userInput").value;
const botResponse = await sendMessage(userInput);
document.getElementById("chatBox").innerHTML += `<p><b>You:</b> ${userInput}</p>`;
document.getElementById("chatBox").innerHTML += `<p><b>AI:</b> ${botResponse}</p>`;
});
```
---
### **3. Update Your HTML**
Make sure your HTML has elements like these:
```html
<div id="chatBox"></div>
<input type="text" id="userInput" placeholder="Type a message...">
<button id="sendBtn">Send</button>
```
---
### **4. Start Everything Up**
1. Run **Ollama**:
```sh
ollama serve
```
2. Open your **HTML file** in a browser.
3. Type a message and hit **Send**—you should see the AI respond!
---
### **Bonus: Handling CORS (If Needed)**
If your browser blocks requests to `http://localhost:11434`, you might need to run a simple **CORS proxy** or use a backend (e.g., Express.js or Flask) to forward requests.
Let me know if you need help with that! 🚀
gpt-4o