altbdoor nsarrazin HF Staff commited on
Commit
dffaafd
·
unverified ·
1 Parent(s): 96070f4

Move Google GenAI API key to env file instead (#1448)

Browse files

- 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

Co-authored-by: Nathan Sarrazin <[email protected]>

.env CHANGED
@@ -16,6 +16,7 @@ ANTHROPIC_API_KEY=#your anthropic api key here
16
  CLOUDFLARE_ACCOUNT_ID=#your cloudflare account id here
17
  CLOUDFLARE_API_TOKEN=#your cloudflare api token here
18
  COHERE_API_TOKEN=#your cohere api token here
 
19
 
20
  HF_ACCESS_TOKEN=#LEGACY! Use HF_TOKEN instead
21
 
 
16
  CLOUDFLARE_ACCOUNT_ID=#your cloudflare account id here
17
  CLOUDFLARE_API_TOKEN=#your cloudflare api token here
18
  COHERE_API_TOKEN=#your cohere api token here
19
+ GOOGLE_GENAI_API_KEY=#your google genai api token here
20
 
21
  HF_ACCESS_TOKEN=#LEGACY! Use HF_TOKEN instead
22
 
docs/source/configuration/models/providers/google.md CHANGED
@@ -52,7 +52,11 @@ MODELS=`[
52
 
53
  Or use the Gemini API API provider [from](https://github.com/google-gemini/generative-ai-js#readme):
54
 
55
- > 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).
 
 
 
 
56
 
57
  ```ini
58
  MODELS=`[
@@ -63,12 +67,12 @@ MODELS=`[
63
  "endpoints": [
64
  {
65
  "type": "genai",
 
 
66
  "apiKey": "abc...xyz"
 
67
  }
68
  ]
69
-
70
- // Optional
71
- "safetyThreshold": "BLOCK_MEDIUM_AND_ABOVE",
72
  },
73
  {
74
  "name": "gemini-1.5-pro",
@@ -77,6 +81,8 @@ MODELS=`[
77
  "endpoints": [
78
  {
79
  "type": "genai",
 
 
80
  "apiKey": "abc...xyz"
81
  }
82
  ]
 
52
 
53
  Or use the Gemini API API provider [from](https://github.com/google-gemini/generative-ai-js#readme):
54
 
55
+ 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).
56
+
57
+ 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.
58
+
59
+ 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).
60
 
61
  ```ini
62
  MODELS=`[
 
67
  "endpoints": [
68
  {
69
  "type": "genai",
70
+
71
+ // Optional
72
  "apiKey": "abc...xyz"
73
+ "safetyThreshold": "BLOCK_MEDIUM_AND_ABOVE",
74
  }
75
  ]
 
 
 
76
  },
77
  {
78
  "name": "gemini-1.5-pro",
 
81
  "endpoints": [
82
  {
83
  "type": "genai",
84
+
85
+ // Optional
86
  "apiKey": "abc...xyz"
87
  }
88
  ]
src/lib/server/endpoints/google/endpointGenAI.ts CHANGED
@@ -1,17 +1,18 @@
1
  import { GoogleGenerativeAI, HarmBlockThreshold, HarmCategory } from "@google/generative-ai";
2
- import type { Content, Part, TextPart } from "@google/generative-ai";
3
  import { z } from "zod";
4
  import type { Message, MessageFile } from "$lib/types/Message";
5
  import type { TextGenerationStreamOutput } from "@huggingface/inference";
6
  import type { Endpoint } from "../endpoints";
7
  import { createImageProcessorOptionsValidator, makeImageProcessor } from "../images";
8
  import type { ImageProcessorOptions } from "../images";
 
9
 
10
  export const endpointGenAIParametersSchema = z.object({
11
  weight: z.number().int().positive().default(1),
12
  model: z.any(),
13
  type: z.literal("genai"),
14
- apiKey: z.string(),
15
  safetyThreshold: z
16
  .enum([
17
  HarmBlockThreshold.HARM_BLOCK_THRESHOLD_UNSPECIFIED,
@@ -40,35 +41,24 @@ export function endpointGenAI(input: z.input<typeof endpointGenAIParametersSchem
40
 
41
  const genAI = new GoogleGenerativeAI(apiKey);
42
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  return async ({ messages, preprompt, generateSettings }) => {
44
  const parameters = { ...model.parameters, ...generateSettings };
45
 
46
  const generativeModel = genAI.getGenerativeModel({
47
  model: model.id ?? model.name,
48
- safetySettings: safetyThreshold
49
- ? [
50
- {
51
- category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
52
- threshold: safetyThreshold,
53
- },
54
- {
55
- category: HarmCategory.HARM_CATEGORY_HARASSMENT,
56
- threshold: safetyThreshold,
57
- },
58
- {
59
- category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
60
- threshold: safetyThreshold,
61
- },
62
- {
63
- category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
64
- threshold: safetyThreshold,
65
- },
66
- {
67
- category: HarmCategory.HARM_CATEGORY_UNSPECIFIED,
68
- threshold: safetyThreshold,
69
- },
70
- ]
71
- : undefined,
72
  generationConfig: {
73
  maxOutputTokens: parameters?.max_new_tokens ?? 4096,
74
  stopSequences: parameters?.stop,
 
1
  import { GoogleGenerativeAI, HarmBlockThreshold, HarmCategory } from "@google/generative-ai";
2
+ import type { Content, Part, SafetySetting, TextPart } from "@google/generative-ai";
3
  import { z } from "zod";
4
  import type { Message, MessageFile } from "$lib/types/Message";
5
  import type { TextGenerationStreamOutput } from "@huggingface/inference";
6
  import type { Endpoint } from "../endpoints";
7
  import { createImageProcessorOptionsValidator, makeImageProcessor } from "../images";
8
  import type { ImageProcessorOptions } from "../images";
9
+ import { env } from "$env/dynamic/private";
10
 
11
  export const endpointGenAIParametersSchema = z.object({
12
  weight: z.number().int().positive().default(1),
13
  model: z.any(),
14
  type: z.literal("genai"),
15
+ apiKey: z.string().default(env.GOOGLE_GENAI_API_KEY),
16
  safetyThreshold: z
17
  .enum([
18
  HarmBlockThreshold.HARM_BLOCK_THRESHOLD_UNSPECIFIED,
 
41
 
42
  const genAI = new GoogleGenerativeAI(apiKey);
43
 
44
+ const safetySettings = safetyThreshold
45
+ ? Object.keys(HarmCategory)
46
+ .filter((cat) => cat !== HarmCategory.HARM_CATEGORY_UNSPECIFIED)
47
+ .reduce((acc, val) => {
48
+ acc.push({
49
+ category: val as HarmCategory,
50
+ threshold: safetyThreshold,
51
+ });
52
+ return acc;
53
+ }, [] as SafetySetting[])
54
+ : undefined;
55
+
56
  return async ({ messages, preprompt, generateSettings }) => {
57
  const parameters = { ...model.parameters, ...generateSettings };
58
 
59
  const generativeModel = genAI.getGenerativeModel({
60
  model: model.id ?? model.name,
61
+ safetySettings,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  generationConfig: {
63
  maxOutputTokens: parameters?.max_new_tokens ?? 4096,
64
  stopSequences: parameters?.stop,