Skip to content

Commit

Permalink
Move Google GenAI API key to env file instead
Browse files Browse the repository at this point in the history
- Updated the documentation with the latest links from Google AI Studio
- Fixed the incorrect documentation on `safetyThreshold`
- Fixed the `safetySettings` to exclude unspecified category, as it is
  not considered as a valid input category in Google's API
  • Loading branch information
altbdoor committed Sep 4, 2024
1 parent 6cfc3a7 commit 719b1e6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ANTHROPIC_API_KEY=#your anthropic api key here
CLOUDFLARE_ACCOUNT_ID=#your cloudflare account id here
CLOUDFLARE_API_TOKEN=#your cloudflare api token here
COHERE_API_TOKEN=#your cohere api token here
GOOGLE_GENAI_API_KEY=#your google genai api token here

HF_ACCESS_TOKEN=#LEGACY! Use HF_TOKEN instead

Expand Down
14 changes: 10 additions & 4 deletions docs/source/configuration/models/providers/google.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ MODELS=`[

Or use the Gemini API API provider [from](https://github.com/google-gemini/generative-ai-js#readme):

> Make sure that you have an API key from Google Cloud Platform. To get an API key, follow the instructions [here](https://cloud.google.com/docs/authentication/api-keys).
Make sure that you have an API key from Google Cloud Platform. To get an API key, follow the instructions [here](https://ai.google.dev/gemini-api/docs/api-key).

You can either specify them directly in your `.env.local` using the `GOOGLE_GENAI_API_KEY` variables, or you can set them directly in the endpoint config.

You can find the list of models available [here](https://ai.google.dev/gemini-api/docs/models/gemini), and experimental models available [here](https://ai.google.dev/gemini-api/docs/models/experimental-models).

```ini
MODELS=`[
Expand All @@ -63,12 +67,12 @@ MODELS=`[
"endpoints": [
{
"type": "genai",

// Optional
"apiKey": "abc...xyz"
"safetyThreshold": "BLOCK_MEDIUM_AND_ABOVE",
}
]

// Optional
"safetyThreshold": "BLOCK_MEDIUM_AND_ABOVE",
},
{
"name": "gemini-1.5-pro",
Expand All @@ -77,6 +81,8 @@ MODELS=`[
"endpoints": [
{
"type": "genai",

// Optional
"apiKey": "abc...xyz"
}
]
Expand Down
42 changes: 16 additions & 26 deletions src/lib/server/endpoints/google/endpointGenAI.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { GoogleGenerativeAI, HarmBlockThreshold, HarmCategory } from "@google/generative-ai";
import type { Content, Part, TextPart } from "@google/generative-ai";
import type { Content, Part, SafetySetting, TextPart } from "@google/generative-ai";
import { z } from "zod";
import type { Message, MessageFile } from "$lib/types/Message";
import type { TextGenerationStreamOutput } from "@huggingface/inference";
import type { Endpoint } from "../endpoints";
import { createImageProcessorOptionsValidator, makeImageProcessor } from "../images";
import type { ImageProcessorOptions } from "../images";
import { env } from "$env/dynamic/private";

export const endpointGenAIParametersSchema = z.object({
weight: z.number().int().positive().default(1),
model: z.any(),
type: z.literal("genai"),
apiKey: z.string(),
apiKey: z.string().default(env.GOOGLE_GENAI_API_KEY),
safetyThreshold: z
.enum([
HarmBlockThreshold.HARM_BLOCK_THRESHOLD_UNSPECIFIED,
Expand Down Expand Up @@ -40,35 +41,24 @@ export function endpointGenAI(input: z.input<typeof endpointGenAIParametersSchem

const genAI = new GoogleGenerativeAI(apiKey);

const safetySettings = safetyThreshold
? Object.keys(HarmCategory)
.filter((cat) => cat !== HarmCategory.HARM_CATEGORY_UNSPECIFIED)
.reduce((acc, val) => {
acc.push({
category: val as HarmCategory,
threshold: safetyThreshold,
});
return acc;
}, [] as SafetySetting[])
: undefined;

return async ({ messages, preprompt, generateSettings }) => {
const parameters = { ...model.parameters, ...generateSettings };

const generativeModel = genAI.getGenerativeModel({
model: model.id ?? model.name,
safetySettings: safetyThreshold
? [
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: safetyThreshold,
},
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: safetyThreshold,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: safetyThreshold,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: safetyThreshold,
},
{
category: HarmCategory.HARM_CATEGORY_UNSPECIFIED,
threshold: safetyThreshold,
},
]
: undefined,
safetySettings,
generationConfig: {
maxOutputTokens: parameters?.max_new_tokens ?? 4096,
stopSequences: parameters?.stop,
Expand Down

0 comments on commit 719b1e6

Please sign in to comment.