Skip to content

Commit

Permalink
Update package version to 1.0.12 (#103)
Browse files Browse the repository at this point in the history
Add support for AzureOpenAI in promptlayer.run
  • Loading branch information
abubakarsohail authored Oct 15, 2024
1 parent 901d71c commit 990a5c8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
24 changes: 16 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "promptlayer",
"license": "MIT",
"version": "1.0.12",
"version": "1.0.13",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
Expand All @@ -18,7 +18,7 @@
"devDependencies": {
"@anthropic-ai/sdk": "^0.20.8",
"@types/node": "^20.8.0",
"openai": "^4.48.1",
"openai": "^4.67.3",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
Expand Down
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
anthropicRequest,
anthropicStreamCompletion,
anthropicStreamMessage,
azureOpenAIRequest,
openaiRequest,
openaiStreamChat,
openaiStreamCompletion,
Expand Down Expand Up @@ -40,11 +41,22 @@ const MAP_PROVIDER_TO_FUNCTION_NAME = {
stream_function: anthropicStreamCompletion,
},
},
"openai.azure": {
chat: {
function_name: "openai.AzureOpenAI.chat.completions.create",
stream_function: openaiStreamChat,
},
completion: {
function_name: "openai.AzureOpenAI.completions.create",
stream_function: openaiStreamCompletion,
},
},
};

const MAP_PROVIDER_TO_FUNCTION: Record<string, any> = {
openai: openaiRequest,
anthropic: anthropicRequest,
"openai.azure": azureOpenAIRequest,
};

export interface ClientOptions {
Expand Down Expand Up @@ -184,7 +196,7 @@ export class PromptLayer {
kwargs["baseURL"] = provider_base_url.url;
}
kwargs["stream"] = stream;
if (stream && provider_type === "openai") {
if (stream && ["openai", "openai.azure"].includes(provider_type)) {
kwargs["stream_options"] = { include_usage: true };
}

Expand Down
30 changes: 23 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ const getPromptTemplate = async (
);
return null;
}
if(data.warning){
console.warn(
`WARNING: While tracking your prompt PromptLayer had the following error: ${data.warning}`
);
if (data.warning) {
console.warn(
`WARNING: While tracking your prompt PromptLayer had the following error: ${data.warning}`
);
}
return data as Promise<GetPromptTemplateResponse>;
} catch (e) {
Expand Down Expand Up @@ -439,15 +439,17 @@ const openaiStreamChat = (results: ChatCompletionChunk[]): ChatCompletion => {
}`;
}
}
const firstChoice = results[0].choices.at(0);
response.choices.push({
finish_reason: results[0].choices[0].finish_reason || "stop",
index: results[0].choices[0].index || 0,
logprobs: results[0].choices[0].logprobs || null,
finish_reason: firstChoice?.finish_reason ?? "stop",
index: firstChoice?.index ?? 0,
logprobs: firstChoice?.logprobs ?? null,
message: {
role: "assistant",
content,
function_call: functionCall ? functionCall : undefined,
tool_calls: toolCalls ? toolCalls : undefined,
refusal: firstChoice?.delta.refusal ?? null,
},
});
response.id = lastResult.id;
Expand Down Expand Up @@ -706,6 +708,19 @@ const openaiRequest = async (
return requestToMake(client, kwargs);
};

const azureOpenAIRequest = async (
promptBlueprint: GetPromptTemplateResponse,
kwargs: any
) => {
const OpenAI = require("openai").AzureOpenAI;
const client = new OpenAI({
baseURL: kwargs.baseURL,
});
const requestToMake =
MAP_TYPE_TO_OPENAI_FUNCTION[promptBlueprint.prompt_template.type];
return requestToMake(client, kwargs);
};

const anthropicChatRequest = async (client: TypeAnthropic, kwargs: any) => {
return client.messages.create(kwargs);
};
Expand Down Expand Up @@ -768,6 +783,7 @@ export {
anthropicRequest,
anthropicStreamCompletion,
anthropicStreamMessage,
azureOpenAIRequest,
getAllPromptTemplates,
getPromptTemplate,
openaiRequest,
Expand Down

0 comments on commit 990a5c8

Please sign in to comment.