Skip to content

Commit

Permalink
fixing-lib (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Raghuwanshi authored Oct 1, 2024
1 parent 9405d62 commit cf002d9
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions JS/edgechains/arakoodev/src/ai/src/lib/openai/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ const openAI_url = "https://api.openai.com/v1/chat/completions";

interface OpenAIConstructionOptions {
apiKey?: string;
orgId?: string;
}

interface messageOption {
role: role;
content: string;
name?: string;
}
[];

interface OpenAIChatOptions {
model?: ChatModel;
role?: role;
max_tokens?: number;
temperature?: number;
prompt?: string;
messages?: messageOption;
messages?: messageOption[];
}

interface chatWithFunctionOptions {
Expand All @@ -31,7 +31,7 @@ interface chatWithFunctionOptions {
temperature?: number;
prompt?: string;
functions?: object | Array<object>;
messages?: messageOption;
messages?: messageOption[];
function_call?: string;
}

Expand All @@ -58,23 +58,35 @@ interface OpenAIChatReturnOptions {

export class OpenAI {
apiKey: string;
orgId: string;
constructor(options: OpenAIConstructionOptions) {
this.apiKey = options.apiKey || process.env.OPENAI_API_KEY || "";
this.orgId = options.orgId || process.env.OPENAI_ORG_ID || "";
this.checkKeys();
}

async chat(chatOptions: OpenAIChatOptions): Promise<OpenAIChatReturnOptions> {
private checkKeys(): void {
if (!this.apiKey) {
console.error("API key is missing. Please provide a valid OpenAI API key. You can add it in .env file as OPENAI_API_KEY");
}
if (!this.orgId) {
console.warn("Organization ID is missing. Please provide a valid OpenAI Organization ID. You can add it in .env file as OPENAI_ORG_ID");
}
}

async chat (chatOptions: OpenAIChatOptions): Promise<OpenAIChatReturnOptions> {
const response = await axios
.post(
openAI_url,
{
model: chatOptions.model || "gpt-3.5-turbo",
messages: chatOptions.prompt
? [
{
role: chatOptions.role || "user",
content: chatOptions.prompt,
},
]
{
role: chatOptions.role || "user",
content: chatOptions.prompt,
},
]
: chatOptions.messages,
max_tokens: chatOptions.max_tokens || 256,
temperature: chatOptions.temperature || 0.7,
Expand All @@ -83,6 +95,7 @@ export class OpenAI {
headers: {
Authorization: "Bearer " + this.apiKey,
"content-type": "application/json",
"OpenAI-Organization": this.orgId,
},
}
)
Expand Down Expand Up @@ -112,11 +125,11 @@ export class OpenAI {
model: chatOptions.model || "gpt-3.5-turbo",
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,
temperature: chatOptions.temperature || 0.7,
Expand All @@ -127,6 +140,7 @@ export class OpenAI {
headers: {
Authorization: "Bearer " + this.apiKey,
"content-type": "application/json",
"OpenAI-Organization": this.orgId,
},
}
)
Expand All @@ -146,18 +160,19 @@ export class OpenAI {
return response[0].message;
}

async generateEmbeddings(resp): Promise<any> {
async generateEmbeddings({ input, model }: { input: string[], model: string }): Promise<any> {
const response = await axios
.post(
"https://api.openai.com/v1/embeddings",
{
model: "text-embedding-ada-002",
input: resp,
model: model,
input,
},
{
headers: {
Authorization: `Bearer ${this.apiKey}`,
"content-type": "application/json",
"OpenAI-Organization": this.orgId,
},
}
)
Expand Down Expand Up @@ -215,6 +230,7 @@ export class OpenAI {
headers: {
Authorization: "Bearer " + this.apiKey,
"content-type": "application/json",
"OpenAI-Organization": this.orgId,
},
}
)
Expand Down

0 comments on commit cf002d9

Please sign in to comment.