-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa99f0c
commit ecae091
Showing
9 changed files
with
1,361 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import OpenAI from "openai"; | ||
// const { GoogleGenerativeAI } = require("@google/generative-ai"); | ||
import { GoogleGenerativeAI } from "@google/generative-ai"; | ||
const genAI = new GoogleGenerativeAI(process.env.GEMINI_KEY as string); | ||
const model = genAI.getGenerativeModel({ model: "gemini-pro" }); | ||
import Anthropic from "@anthropic-ai/sdk"; | ||
|
||
const openai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
}); | ||
|
||
const anthropic = new Anthropic({ | ||
apiKey: process.env.ANTHROPIC_KEY, // defaults to process.env["ANTHROPIC_API_KEY"] | ||
}); | ||
|
||
export function errorResponse(callback, err) { | ||
console.error(err); | ||
|
||
callback(null, { | ||
statusCode: 500, | ||
body: JSON.stringify({ error: err }), | ||
}); | ||
} | ||
|
||
const engines = { | ||
google: (prompt: string) => model.generateContent(prompt), | ||
openai: (prompt: string) => | ||
openai.chat.completions.create({ | ||
messages: [{ role: "user", content: prompt }], | ||
// model: "gpt-3.5-turbo", | ||
n: 1, | ||
temperature: 0, | ||
model: "gpt-4o", | ||
// model: "gpt-4", | ||
// model: "gpt-4-turbo-preview", | ||
}), | ||
anthropic: (prompt: string) => | ||
anthropic.messages.create({ | ||
// model: "claude-3-opus-20240229", | ||
model: "claude-3-haiku-20240307", | ||
// model: "claude-3-sonnet-20240229", | ||
max_tokens: 256, | ||
temperature: 0, | ||
messages: [{ role: "user", content: prompt }], | ||
}), | ||
}; | ||
|
||
export const handler = async (event, _context, callback) => { | ||
let promptInput; | ||
try { | ||
promptInput = event.body; | ||
} catch (e) { | ||
console.log(e); | ||
errorResponse(callback, "Bad submit"); | ||
return; | ||
} | ||
const engine = event.queryStringParameters.engine; | ||
if (!engine) { | ||
errorResponse(callback, "No engine"); | ||
return; | ||
} | ||
if (typeof engine !== "string" || !engines[engine]) { | ||
errorResponse(callback, "Bad engine"); | ||
return; | ||
} | ||
const result = await engines[engine](promptInput); | ||
|
||
console.log(result); | ||
callback(null, { statusCode: 200, body: JSON.stringify(result) }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.