Compatibility requirements for OpenAI compatible API translation layer? #1983
-
|
Hello everyone, The following code currently works in OpenWebUI but I can’t get it to work in OmniRoute where I tried to add my translation layer via the "Add OpenAI Compatible" button. I did not specify an API key because I’m providing it through an environment variable. require("dotenv").config();
import express from "express";
import fetch from "node-fetch";
const app = express();
app.use(express.json());
const API_KEY = process.env.API_KEY;
const ALLOWED_MODELS = [
"gpt-4o-mini",
"gpt-4o"
];
app.post("/v1/chat/completions", async (req, res) => {
try {
const { messages, model } = req.body;
const prompt = messages
.map((m) => {
if (m.role === "system") return `System: ${m.content}`;
if (m.role === "user") return `User: ${m.content}`;
if (m.role === "assistant") return `Assistant: ${m.content}`;
return "";
})
.join("\n");
const response = await fetch("https://api.1min.ai/api/chat-with-ai?isStreaming=false", {
method: "POST",
headers: {
"API-KEY": API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
type: "UNIFY_CHAT_WITH_AI",
model: model,
promptObject: {
prompt,
settings: {
webSearchSettings: { webSearch: false },
},
},
}),
});
const data = await response.json();
const content = data?.aiRecord?.aiRecordDetail?.resultObject?.[0] || "No response";
res.json({
id: `chatcmpl-${Date.now()}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model: model,
choices: [
{
index: 0,
message: {
role: "assistant",
content,
},
finish_reason: "stop",
},
],
});
} catch (err) {
console.error(err);
res.status(500).json({ error: "Proxy error" });
}
});
app.get("/v1/models", (req, res) => {
res.json({
object: "list",
data: ALLOWED_MODELS.map((m) => ({
id: m,
object: "model",
})),
});
});
app.listen(3001, () => {
console.log("API Proxy running on port 3001");
});Could anyone help point me towards a solution? I am not sure whether this is a problem of me not setting the provider up correctly or whether my endpoints are returning an incompatible format that OmniRoute can’t process correctly? Thanks very much in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
Hi @timjuedemann! Your translation layer looks correct for the response format. A few things to check when using it with OmniRoute:
|
Beta Was this translation helpful? Give feedback.
-
|
Hello @diegosouzapw, thanks for taking the time to help me with this. I should have mentioned that I’m running OmniRoute on a local homelab style server because that probably would have cleared up my mistake immediately. Instead, I had to learn the hard way that OmniRoute is set to block private/local IPs for security reasons per default. Fortunately, adding Best regards, |
Beta Was this translation helpful? Give feedback.
-
|
Glad you got it working, @timjuedemann! The |
Beta Was this translation helpful? Give feedback.
Hi @timjuedemann!
Your translation layer looks correct for the response format. A few things to check when using it with OmniRoute: