Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Azure AI #352

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add azure
  • Loading branch information
Phiph committed Oct 18, 2024
commit c1cec504e96cad281ef3c534adf865dd925f134a
22 changes: 18 additions & 4 deletions packages/code-review-gpt/src/common/model/AIModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAIChat } from "langchain/llms/openai";
import { ChatOpenAI, AzureChatOpenAI } from "@langchain/openai";
import { retryAsync } from "ts-retry";

import { IFeedback } from "../types";
Expand All @@ -17,13 +17,13 @@ interface IAIModel {
const defaultRetryCount = 3;

class AIModel {
private model: OpenAIChat;
private model: ChatOpenAI;
private retryCount: number;

constructor(options: IAIModel) {
switch (options.provider) {
case "openai":
this.model = new OpenAIChat({
this.model = new ChatOpenAI({
openAIApiKey: options.apiKey,
modelName: options.modelName,
temperature: options.temperature,
Expand All @@ -32,6 +32,13 @@ class AIModel {
break;
case "bedrock":
throw new Error("Bedrock provider not implemented");
case "azure":
this.model = new AzureChatOpenAI({
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The settings are not set here, as you can use the EnvVars and the package will pick those up by default.

e.g:

OPENAI_API_KEY=
AZURE_OPENAI_API_INSTANCE_NAME="oai-4turbo"
AZURE_OPENAI_API_DEPLOYMENT_NAME="gpt-4-turbo"
AZURE_OPENAI_API_KEY=xxxx
AZURE_OPENAI_API_VERSION="2024-08-01-preview"

openAIApiKey: options.apiKey,
modelName: options.modelName,
temperature: options.temperature,
configuration: { organization: options.organization },
});
default:
throw new Error("Provider not supported");
}
Expand All @@ -40,9 +47,16 @@ class AIModel {
}

public async callModel(prompt: string): Promise<string> {
return this.model.call(prompt);
const messages = [{ role: "user", content: prompt }];
const response = await this.model.invoke(messages);
if (typeof response.content === 'string') {
return response.content;
} else {
throw new Error('Response content is not a string');
}
}


public async callModelJSON(prompt: string): Promise<IFeedback[]> {
return retryAsync(
async () => {
Expand Down