Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Adding Ollama support #99

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env-ollama.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OPENAI_API_KEY=sk-1234567890abcdef
OPENAI_BASE_URL=http://localhost:11434/v1
OPENAI_MODEL='llava:7b'
1 change: 1 addition & 0 deletions .env-openai.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY=[sk-your-key]
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

.env
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,28 @@ This works by just taking the current canvas SVG, converting it to a PNG, and se

## Getting Started

This is a Next.js app. To get started run the following commands in the root directory of the project. You will need an OpenAI API key with access to the GPT-4 Vision API.
This is a Next.js app. To get started run the following commands in the root directory of the project.

> Note this uses Next.js 14 and requires a version of `node` greater than 18.17. [Read more here](https://nextjs.org/docs/pages/building-your-application/upgrading/version-14).

### OpenAI

You will need an OpenAI API key with access to the GPT-4 Vision API.

```bash
echo "OPENAI_API_KEY=sk-your-key" > .env.local
cp .env-openai.example .env # Edit the .env with your OpenAI key
npm install
npm run dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

### Ollama

You will need access to an Ollama instance with access to an LLM model with vision capabilities, i.e. llava.

```bash
cp .env-openai.example .env # Edit the .env with your Ollama base url and model name, you can leave the key as is
npm install
npm run dev
```
15 changes: 12 additions & 3 deletions app/api/toHtml/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ const systemPrompt = `You are an expert tailwind developer. A user will provide
if you need to insert an image, use placehold.co to create a placeholder image. Respond only with the html file.`;

export async function POST(request: Request) {
const openai = new OpenAI();
const apiKey = process.env.OPENAI_API_KEY;
const baseURL = process.env.OPENAI_BASE_URL;
const model = process.env.OPENAI_MODEL ?? "gpt-4o";
const max_tokens = process.env.OPENAI_MAX_TOKENS
? parseInt(process.env.OPENAI_MAX_TOKENS, 10)
: 4096;

const openai = new OpenAI({ baseURL, apiKey });

const { image } = await request.json();

const resp = await openai.chat.completions.create({
model: "gpt-4o",
max_tokens: 4096,
model,
max_tokens,
temperature: 0.8,
messages: [
{
role: "system",
Expand Down