-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/adding-llama-and-gemini-classes (#405)
* feat/adding-llama-and-gemini-classes * fixing-gemini
- Loading branch information
1 parent
fb14ff7
commit 4f00857
Showing
10 changed files
with
204 additions
and
137 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
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,3 @@ | ||
export { OpenAI } from "./lib/openai/openai.js"; | ||
export { GeminiAI } from "./lib/gemini/gemini.js"; | ||
export { LlamaAI } from "./lib/llama/llama.js"; |
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,92 @@ | ||
import axios from "axios"; | ||
import { retry } from "@lifeomic/attempt" | ||
const url = "https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent"; | ||
|
||
interface GeminiAIConstructionOptions { | ||
apiKey?: string; | ||
} | ||
|
||
type SafetyRating = { | ||
category: "HARM_CATEGORY_SEXUALLY_EXPLICIT" | "HARM_CATEGORY_HATE_SPEECH" | "HARM_CATEGORY_HARASSMENT" | "HARM_CATEGORY_DANGEROUS_CONTENT"; | ||
probability: "NEGLIGIBLE" | "LOW" | "MEDIUM" | "HIGH"; | ||
}; | ||
|
||
type ContentPart = { | ||
text: string; | ||
}; | ||
|
||
type Content = { | ||
parts: ContentPart[]; | ||
role: string; | ||
}; | ||
|
||
type Candidate = { | ||
content: Content; | ||
finishReason: string; | ||
index: number; | ||
safetyRatings: SafetyRating[]; | ||
}; | ||
|
||
type UsageMetadata = { | ||
promptTokenCount: number; | ||
candidatesTokenCount: number; | ||
totalTokenCount: number; | ||
}; | ||
|
||
type Response = { | ||
candidates: Candidate[]; | ||
usageMetadata: UsageMetadata; | ||
}; | ||
|
||
|
||
type responseMimeType = "text/plain" | "application/json" | ||
|
||
|
||
interface GeminiAIChatOptions { | ||
model?: string; | ||
max_output_tokens?: number; | ||
temperature?: number; | ||
prompt: string; | ||
max_retry?: number; | ||
responseType?: responseMimeType; | ||
delay?: number | ||
} | ||
|
||
export class GeminiAI { | ||
apiKey: string; | ||
constructor(options: GeminiAIConstructionOptions) { | ||
this.apiKey = options.apiKey || process.env.GEMINI_API_KEY || ""; | ||
} | ||
|
||
async chat(chatOptions: GeminiAIChatOptions): Promise<Response> { | ||
let data = JSON.stringify({ | ||
"contents": [ | ||
{ | ||
"role": "user", | ||
"parts": [ | ||
{ | ||
"text": chatOptions.prompt | ||
} | ||
] | ||
} | ||
] | ||
}); | ||
|
||
let config = { | ||
method: 'post', | ||
maxBodyLength: Infinity, | ||
url, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-goog-api-key': this.apiKey | ||
}, | ||
temperature: chatOptions.temperature || "0.7", | ||
responseMimeType: chatOptions.responseType || "text/plain", | ||
"max_output_tokens": chatOptions.max_output_tokens || 1024, | ||
data: data | ||
}; | ||
return await retry(async () => { | ||
return (await axios.request(config)).data; | ||
}, { maxAttempts: chatOptions.max_retry || 3, delay: chatOptions.delay || 200 }); | ||
} | ||
} |
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,103 @@ | ||
|
||
import axios from "axios"; | ||
import { role } from "../../types"; | ||
import { retry } from "@lifeomic/attempt"; | ||
|
||
const url = 'https://api.llama-api.com/chat/completions' | ||
|
||
interface messageOption { | ||
role: role; | ||
content: string; | ||
name?: string; | ||
} | ||
|
||
interface llamaChatOptions { | ||
model?: string; | ||
role?: role; | ||
max_tokens?: number; | ||
temperature?: number; | ||
prompt?: string; | ||
messages?: messageOption[]; | ||
stream?: boolean | ||
max_retry?: number; | ||
delay?: number | ||
}[] | ||
|
||
export class LlamaAI { | ||
apiKey: string | ||
queue: string[] | ||
constructor({ apiKey }: { apiKey: string }) { | ||
this.apiKey = apiKey; | ||
this.queue = []; | ||
} | ||
|
||
async makeRequest(chatOptions: llamaChatOptions) { | ||
try { | ||
return await retry(async () => { | ||
|
||
return await axios | ||
.post( | ||
url, | ||
{ | ||
model: chatOptions.model || "llama-13b-chat", | ||
messages: chatOptions.prompt | ||
? [ | ||
{ | ||
role: chatOptions.role || "user", | ||
content: chatOptions.prompt, | ||
}, | ||
] | ||
: chatOptions.messages, | ||
max_tokens: chatOptions.max_tokens || 1024, | ||
stream: chatOptions.stream || false, | ||
temperature: chatOptions.temperature || 0.7, | ||
}, | ||
{ | ||
headers: { Authorization: "Bearer " + this.apiKey }, | ||
} | ||
) | ||
}, { maxAttempts: chatOptions.max_retry || 3, delay: chatOptions.delay || 200 }); | ||
} catch (error: any) { | ||
console.log(error) | ||
throw new Error(`Error while making request: ${error.message}`); | ||
} | ||
} | ||
|
||
async _runStreamForJupyter(apiRequestJson) { | ||
const response = await this.makeRequest(apiRequestJson); | ||
|
||
for (const chunk of response.data) { | ||
this.queue.push(chunk); | ||
} | ||
} | ||
|
||
async *getSequences() { | ||
while (this.queue.length > 0) { | ||
yield this.queue.shift(); | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
} | ||
} | ||
|
||
async runStream(apiRequestJson) { | ||
await this._runStreamForJupyter(apiRequestJson); | ||
this.getSequences(); | ||
} | ||
|
||
async runSync(apiRequestJson) { | ||
const response = await this.makeRequest(apiRequestJson); | ||
|
||
if (response.status !== 200) { | ||
throw new Error(`POST ${response.status} ${response.data.detail}`); | ||
} | ||
|
||
return response.data; | ||
} | ||
|
||
chat(chatOptions: llamaChatOptions) { | ||
if (chatOptions.stream) { | ||
return this.runStream(chatOptions); | ||
} else { | ||
return this.runSync(chatOptions); | ||
} | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
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 was deleted.
Oops, something went wrong.
129 changes: 0 additions & 129 deletions
129
JS/edgechains/arakoodev/src/openai/src/lib/streaming/OpenAiStreaming.ts
This file was deleted.
Oops, something went wrong.