Skip to content

Commit d755a2b

Browse files
committed
fix(ollama): support for OPTIONS and GET requests
Add handling for OPTIONS requests to check supported API methods and implement GET requests to retrieve the list of models. Signed-off-by: pedh <[email protected]>
1 parent c8b31b8 commit d755a2b

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

proxy.ts

+31-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Hono } from "@hono/hono";
22
import { bearerAuth } from "@hono/hono/bearer-auth";
33
import { assert, is } from "@core/unknownutil";
44
import { parseURL } from "ufo";
5-
65
import { chooseEndpoint, convertToCustomEndpoint } from "./util.ts";
76

87
export function createApp(
@@ -18,15 +17,25 @@ export function createApp(
1817
) {
1918
const app = new Hono();
2019

21-
if (is.String(OPENAI_API_KEY)) {
22-
app.use("*", bearerAuth({ token: OPENAI_API_KEY }));
23-
}
20+
// Apply bearer authentication, but skip it for OPTIONS requests
21+
app.use((c, next) => {
22+
if (c.req.method !== "OPTIONS") {
23+
if (is.String(OPENAI_API_KEY)) {
24+
return bearerAuth({ token: OPENAI_API_KEY })(c, next);
25+
}
26+
}
27+
// If the method is OPTIONS, skip the bearerAuth
28+
return next();
29+
});
2430

31+
// Handle POST requests
2532
app.post("*", async (c) => {
2633
const json = await c.req.raw.clone().json();
2734
const { model } = json;
28-
/** if model name starts with "gpt" then it is a chat model */
35+
36+
// Validate the request payload
2937
assert(json, is.ObjectOf({ model: is.String }));
38+
3039
const endpoint = chooseEndpoint({
3140
model,
3241
ollamaEndpoint,
@@ -35,10 +44,27 @@ export function createApp(
3544

3645
const url = convertToCustomEndpoint(c.req.url, parseURL(endpoint));
3746
const req = new Request(url, c.req.raw);
47+
req.headers.set("Host", ollamaEndpoint);
48+
return fetch(req);
49+
});
3850

51+
// Handle GET requests
52+
app.get("*", async (c) => {
53+
const url = convertToCustomEndpoint(c.req.url, parseURL(ollamaEndpoint));
54+
const req = new Request(url, c.req.raw);
3955
req.headers.set("Host", ollamaEndpoint);
4056
return fetch(req);
4157
});
4258

59+
// Handle OPTIONS requests
60+
app.options("*", (c) => {
61+
c.header("Allow", "OPTIONS, GET, POST");
62+
c.header("Access-Control-Allow-Origin", "*");
63+
c.header("Access-Control-Allow-Methods", "OPTIONS, GET, POST");
64+
c.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
65+
return c.body(null, 204);
66+
});
67+
4368
return app;
4469
}
70+

0 commit comments

Comments
 (0)