Skip to content

Commit

Permalink
Merge branch 'ts' into updating-rpa-challenge-and-playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Raghuwanshi authored Aug 14, 2024
2 parents dda1bc4 + 7ba0722 commit b3705d8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 45 deletions.
49 changes: 27 additions & 22 deletions JS/edgechains/arakoodev/src/ai/src/lib/gemini/gemini.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import axios from "axios";
import { retry } from "@lifeomic/attempt"
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";
category:
| "HARM_CATEGORY_SEXUALLY_EXPLICIT"
| "HARM_CATEGORY_HATE_SPEECH"
| "HARM_CATEGORY_HARASSMENT"
| "HARM_CATEGORY_DANGEROUS_CONTENT";
probability: "NEGLIGIBLE" | "LOW" | "MEDIUM" | "HIGH";
};

Expand Down Expand Up @@ -38,9 +42,7 @@ type Response = {
usageMetadata: UsageMetadata;
};


type responseMimeType = "text/plain" | "application/json"

type responseMimeType = "text/plain" | "application/json";

interface GeminiAIChatOptions {
model?: string;
Expand All @@ -49,7 +51,7 @@ interface GeminiAIChatOptions {
prompt: string;
max_retry?: number;
responseType?: responseMimeType;
delay?: number
delay?: number;
}

export class GeminiAI {
Expand All @@ -60,33 +62,36 @@ export class GeminiAI {

async chat(chatOptions: GeminiAIChatOptions): Promise<Response> {
let data = JSON.stringify({
"contents": [
contents: [
{
"role": "user",
"parts": [
role: "user",
parts: [
{
"text": chatOptions.prompt
}
]
}
]
text: chatOptions.prompt,
},
],
},
],
});

let config = {
method: 'post',
method: "post",
maxBodyLength: Infinity,
url,
headers: {
'Content-Type': 'application/json',
'x-goog-api-key': this.apiKey
"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
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 });
return await retry(
async () => {
return (await axios.request(config)).data;
},
{ maxAttempts: chatOptions.max_retry || 3, delay: chatOptions.delay || 200 }
);
}
}
41 changes: 21 additions & 20 deletions JS/edgechains/arakoodev/src/ai/src/lib/llama/llama.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

import axios from "axios";
import { role } from "../../types";
import { retry } from "@lifeomic/attempt";

const url = 'https://api.llama-api.com/chat/completions'
const url = "https://api.llama-api.com/chat/completions";

interface messageOption {
role: role;
Expand All @@ -18,35 +17,35 @@ interface llamaChatOptions {
temperature?: number;
prompt?: string;
messages?: messageOption[];
stream?: boolean
stream?: boolean;
max_retry?: number;
delay?: number
}[]
delay?: number;
}
[];

export class LlamaAI {
apiKey: string
queue: string[]
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(
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,
},
]
{
role: chatOptions.role || "user",
content: chatOptions.prompt,
},
]
: chatOptions.messages,
max_tokens: chatOptions.max_tokens || 1024,
stream: chatOptions.stream || false,
Expand All @@ -55,10 +54,12 @@ export class LlamaAI {
{
headers: { Authorization: "Bearer " + this.apiKey },
}
)
}, { maxAttempts: chatOptions.max_retry || 3, delay: chatOptions.delay || 200 });
);
},
{ maxAttempts: chatOptions.max_retry || 3, delay: chatOptions.delay || 200 }
);
} catch (error: any) {
console.log(error)
console.log(error);
throw new Error(`Error while making request: ${error.message}`);
}
}
Expand All @@ -74,7 +75,7 @@ export class LlamaAI {
async *getSequences() {
while (this.queue.length > 0) {
yield this.queue.shift();
await new Promise(resolve => setTimeout(resolve, 100));
await new Promise((resolve) => setTimeout(resolve, 100));
}
}

Expand Down
2 changes: 1 addition & 1 deletion JS/edgechains/arakoodev/src/ai/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ export type ChatModel =
| "gpt-3.5-turbo-0125"
| "gpt-3.5-turbo-16k-0613";

export type role = "user" | "assistant" | "system";
export type role = "user" | "assistant" | "system";
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"dependencies": {
"@microsoft/eslint-formatter-sarif": "^3.1.0",
"@playwright/test": "^1.46.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"axios": "^1.7.3",
"@typescript-eslint/eslint-plugin": "^8.1.0",
"@typescript-eslint/parser": "^8.1.0",
"eslint": "^8.57.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^9.1.0",
Expand Down

0 comments on commit b3705d8

Please sign in to comment.