Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
johnd0e committed Nov 15, 2024
1 parent 0140a5d commit 02f6601
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
7 changes: 2 additions & 5 deletions readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,17 @@ To utilize it, you should enter your API address and your Gemini API key into th
Typically, you should specify the API base in this format:
`https://my-super-proxy.vercel.app/v1`

However, some software may expect it without the `/v1` ending:
`https://my-super-proxy.vercel.app`

The relevant field may be labeled as "_OpenAI proxy_".
You might need to look under "_Advanced settings_" or similar sections.
Alternatively, it could be in some config file (check the relevant documentation for details).

For some command-line tools, you may need to set an environment variable, _e.g._:
```sh
set OPENAI_BASE_URL=https://my-super-proxy.vercel.app/v1
OPENAI_BASE_URL=https://my-super-proxy.vercel.app/v1
```
_..or_:
```sh
set OPENAI_API_BASE=https://my-super-proxy.vercel.app/v1
OPENAI_API_BASE=https://my-super-proxy.vercel.app/v1
```

## Models
Expand Down
32 changes: 12 additions & 20 deletions src/worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default {
try {
json = await request.json();
if (!Array.isArray(json.messages)) {
throw SyntaxError(".messages array required");
throw new SyntaxError(".messages array required");
}
} catch (err) {
console.error(err.toString());
Expand Down Expand Up @@ -87,7 +87,7 @@ const processModels = (data) => {
const DEFAULT_MODEL = "gemini-1.5-pro-latest";
// https://github.com/google-gemini/generative-ai-js/blob/cf223ff4a1ee5a2d944c53cddb8976136382bee6/src/requests/request.ts#L71
const API_CLIENT = "genai-js/0.19.0"; // npm view @google/generative-ai version
async function handleRequest(req, apiKey) {
async function handleRequest (req, apiKey) {
const model = req.model?.startsWith("gemini-") ? req.model : DEFAULT_MODEL;
const TASK = req.stream ? "streamGenerateContent" : "generateContent";
let url = `${BASE_URL}/${API_VERSION}/models/${model}:${TASK}`;
Expand All @@ -108,13 +108,13 @@ async function handleRequest(req, apiKey) {
return new Response(err, { status: 400, headers: {"Access-Control-Allow-Origin": "*"} });
}

let body;
let body = response.body;
const headers = new Headers(response.headers);
headers.set("Access-Control-Allow-Origin", "*");
if (response.ok) {
let id = generateChatcmplId(); //"chatcmpl-8pMMaqXMK68B3nyDBrapTDrhkHBQK";
if (req.stream) {
body = response.body
body = body
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TransformStream({
transform: parseStream,
Expand All @@ -130,24 +130,13 @@ async function handleRequest(req, apiKey) {
} else {
body = await response.text();
try {
body = await processResponse(JSON.parse(body), model, id);
body = processResponse(JSON.parse(body), model, id);
} catch (err) {
console.error(err);
response = { status: 500 };
headers.set("Content-Type", "text/plain");
}
}
} else {
// Error: [400 Bad Request] User location is not supported for the API use.
body = await response.text();
try {
const { code, status, message } = JSON.parse(body).error;
body = `Error: [${code} ${status}] ${message}`;
} catch (err) {
// pass body as is
}
headers.set("Content-Type", "text/plain");
//headers.delete("Transfer-Encoding");
}
return new Response(body, { status: response.status, statusText: response.statusText, headers });
}
Expand Down Expand Up @@ -191,15 +180,18 @@ const parseImg = async (url) => {
if (url.startsWith("http://") || url.startsWith("https://")) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText} (${url})`);
}
mimeType = response.headers.get("content-type");
data = Buffer.from(await response.arrayBuffer()).toString("base64");
} catch (err) {
throw Error("Error fetching image: " + err.toString());
throw new Error("Error fetching image: " + err.toString());
}
} else {
const match = url.match(/^data:(?<mimeType>.*?)(;base64)?,(?<data>.*)$/);
if (!match) {
throw Error("Invalid image data: " + url);
throw new Error("Invalid image data: " + url);
}
({ mimeType, data } = match.groups);
}
Expand Down Expand Up @@ -233,7 +225,7 @@ const transformMsg = async ({ role, content }) => {
parts.push(await parseImg(item.image_url.url));
break;
default:
throw TypeError(`Unknown "content" item type: "${item.type}"`);
throw new TypeError(`Unknown "content" item type: "${item.type}"`);
}
}
return { role, parts };
Expand Down Expand Up @@ -294,7 +286,7 @@ const transformUsage = (data) => ({
total_tokens: data.totalTokenCount
});

const processResponse = async (data, model, id) => {
const processResponse = (data, model, id) => {
return JSON.stringify({
id,
choices: data.candidates.map(transformCandidatesMessage),
Expand Down

0 comments on commit 02f6601

Please sign in to comment.