-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: adding gemini support * chore: update package lock
- Loading branch information
1 parent
5b8ed3c
commit 5497b29
Showing
6 changed files
with
318 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { init } from '@langtrace-init/init' | ||
import { GoogleGenerativeAI } from '@google/generative-ai' | ||
|
||
import dotenv from 'dotenv' | ||
dotenv.config() | ||
init({ batch: false, write_spans_to_console: true }) | ||
|
||
if (process.env.GEMINI_API_KEY === undefined) { | ||
throw new Error('GEMINI_API_KEY is not defined') | ||
} | ||
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY) | ||
|
||
export const basicGeminiChat = async (): Promise<void> => { | ||
const model = genAI.getGenerativeModel({ | ||
model: 'gemini-1.5-flash', | ||
generationConfig: { | ||
candidateCount: 1, | ||
maxOutputTokens: 20, | ||
temperature: 1.0 | ||
} | ||
}) | ||
const prompt = 'Say hi twice' | ||
const result = await model.generateContent(prompt) | ||
console.log(result.response.text()) | ||
} | ||
|
||
export const basicGeminiChatStream = async (): Promise<void> => { | ||
const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' }) | ||
const prompt = 'say hi 3 times' | ||
const result = await model.generateContentStream(prompt) | ||
|
||
for await (const chunk of result.stream) { | ||
const chunkText = chunk.text() | ||
process.stdout.write(chunkText) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2024 Scale3 Labs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { generateContentPatch } from '@langtrace-instrumentation/gemini/patch' | ||
import { diag } from '@opentelemetry/api' | ||
import { InstrumentationBase, InstrumentationNodeModuleDefinition, isWrapped } from '@opentelemetry/instrumentation' | ||
// eslint-disable-next-line no-restricted-imports | ||
import { name, version } from '../../../package.json' | ||
|
||
class GeminiInstrumentation extends InstrumentationBase<any> { | ||
constructor () { | ||
super(name, version) | ||
} | ||
|
||
init (): Array<InstrumentationNodeModuleDefinition<any>> { | ||
diag.debug('init GeminiInstrumentation') | ||
const module = new InstrumentationNodeModuleDefinition<any>( | ||
'@google/generative-ai', | ||
['>=0.1.3'], | ||
(moduleExports, moduleVersion) => { | ||
diag.debug(`Patching Gemini SDK version ${moduleVersion}`) | ||
this._patch(moduleExports, moduleVersion as string) | ||
return moduleExports | ||
}, | ||
(moduleExports, moduleVersion) => { | ||
diag.debug(`Unpatching Gemini SDK version ${moduleVersion}`) | ||
if (moduleExports !== undefined) { | ||
this._unpatch(moduleExports) | ||
} | ||
} | ||
) | ||
return [module] | ||
} | ||
|
||
private _patch (gemini: any, moduleVersion?: string): void { | ||
if (isWrapped(gemini.GenerativeModel.prototype)) { | ||
this._unpatch(gemini) | ||
} | ||
|
||
this._wrap(gemini.GenerativeModel.prototype, | ||
'generateContent', | ||
(originalMethod: (...args: any[]) => any) => | ||
generateContentPatch(originalMethod, this.tracer, this.instrumentationVersion, name, moduleVersion) | ||
) | ||
|
||
this._wrap(gemini.GenerativeModel.prototype, | ||
'generateContentStream', | ||
(originalMethod: (...args: any[]) => any) => | ||
generateContentPatch(originalMethod, this.tracer, this.instrumentationVersion, name, moduleVersion) | ||
) | ||
} | ||
|
||
private _unpatch (gemini: any): void { | ||
this._unwrap(gemini.GoogleGenerativeAI.GenerativeModel, 'generateContent') | ||
this._unwrap(gemini.GoogleGenerativeAI.GenerativeModel, 'generateContentStream') | ||
} | ||
} | ||
|
||
export const geminiInstrumentation = new GeminiInstrumentation() |
Oops, something went wrong.