-
Notifications
You must be signed in to change notification settings - Fork 7
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
c215120
commit e83bd50
Showing
16 changed files
with
542 additions
and
4 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 |
---|---|---|
|
@@ -28,6 +28,7 @@ words: | |
- longcontext | ||
- openai | ||
- openpose | ||
- permissioning | ||
- qwen | ||
- qwenai | ||
- sdxl | ||
|
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@zhengxs/ai", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "llm sdk", | ||
"keywords": [ | ||
"ai", | ||
|
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,14 @@ | ||
import { GeminiAI } from '../../../src'; | ||
|
||
const ai = new GeminiAI(); | ||
|
||
async function main() { | ||
const chatCompletion = await ai.chat.completions.create({ | ||
model: 'gemini-pro', | ||
messages: [{ role: 'user', content: 'Say this is a test' }], | ||
}); | ||
|
||
console.log(chatCompletion.choices[0].message.content); | ||
} | ||
|
||
main(); |
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,17 @@ | ||
import { GeminiAI } from '../../../src'; | ||
|
||
const client = new GeminiAI(); | ||
|
||
async function main() { | ||
const stream = await client.chat.completions.create({ | ||
stream: true, | ||
model: 'gemini-pro', | ||
messages: [{ role: 'user', content: 'Say this is a test' }], | ||
}); | ||
|
||
for await (const chunk of stream) { | ||
console.log(chunk.choices[0]?.delta?.content || ''); | ||
} | ||
} | ||
|
||
main(); |
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,13 @@ | ||
import { GeminiAI } from '../../../src'; | ||
|
||
const ai = new GeminiAI(); | ||
|
||
async function main() { | ||
const list = await ai.models.list(); | ||
|
||
for await (const model of list) { | ||
console.log(model); | ||
} | ||
} | ||
|
||
main(); |
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,11 @@ | ||
import { GeminiAI } from '../../../src'; | ||
|
||
const ai = new GeminiAI(); | ||
|
||
async function main() { | ||
const model = await ai.models.retrieve('gemini-pro'); | ||
|
||
console.log(model); | ||
} | ||
|
||
main(); |
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,73 @@ | ||
import type { Agent } from 'node:http'; | ||
|
||
import { APIClient, type DefaultQuery, type Fetch, type FinalRequestOptions, type Headers } from 'openai/core'; | ||
|
||
import * as API from './resources'; | ||
|
||
const BASE_URL = 'https://generativelanguage.googleapis.com/v1'; | ||
|
||
export interface GeminiAIOptions { | ||
baseURL?: string; | ||
apiKey?: string; | ||
timeout?: number | undefined; | ||
httpAgent?: Agent; | ||
fetch?: Fetch | undefined; | ||
defaultHeaders?: Headers; | ||
defaultQuery?: DefaultQuery; | ||
} | ||
|
||
export class GeminiAI extends APIClient { | ||
apiKey: string; | ||
|
||
private _options: GeminiAIOptions; | ||
|
||
constructor(options: GeminiAIOptions = {}) { | ||
const { | ||
apiKey = process.env.GEMINI_API_KEY || '', | ||
baseURL = process.env.GEMINI_BASE_URL || BASE_URL, | ||
timeout = 30000, | ||
fetch = globalThis.fetch, | ||
httpAgent = undefined, | ||
...rest | ||
} = options; | ||
|
||
super({ | ||
baseURL, | ||
timeout, | ||
fetch, | ||
httpAgent, | ||
...rest, | ||
}); | ||
|
||
this._options = options; | ||
|
||
this.apiKey = apiKey; | ||
} | ||
|
||
chat = new API.Chat(this); | ||
|
||
models = new API.Models(this); | ||
|
||
protected override defaultHeaders(opts: FinalRequestOptions): Headers { | ||
return { | ||
...super.defaultHeaders(opts), | ||
...this._options.defaultHeaders, | ||
}; | ||
} | ||
|
||
protected override defaultQuery(): DefaultQuery | undefined { | ||
return { | ||
...this._options.defaultQuery, | ||
key: this.apiKey, | ||
}; | ||
} | ||
} | ||
|
||
export namespace GeminiAI { | ||
export type ChatModel = API.ChatModel; | ||
export type ChatCompletionCreateParams = API.ChatCompletionCreateParams; | ||
export type ChatCompletionCreateParamsStreaming = API.ChatCompletionCreateParamsStreaming; | ||
export type ChatCompletionCreateParamsNonStreaming = API.ChatCompletionCreateParamsNonStreaming; | ||
} | ||
|
||
export default GeminiAI; |
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,9 @@ | ||
import { GeminiAI } from './index'; | ||
|
||
export class APIResource { | ||
protected _client: GeminiAI; | ||
|
||
constructor(client: GeminiAI) { | ||
this._client = client; | ||
} | ||
} |
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,6 @@ | ||
import { APIResource } from '../../resource'; | ||
import { Completions } from './completions'; | ||
|
||
export class Chat extends APIResource { | ||
completions = new Completions(this._client); | ||
} |
Oops, something went wrong.