diff --git a/docs/ai-provider-config.md b/docs/ai-provider-config.md new file mode 100644 index 00000000..0abe0111 --- /dev/null +++ b/docs/ai-provider-config.md @@ -0,0 +1,39 @@ +# Provider Configuration + +## Using the Azure OpenAI Provider + +This section will guide you through configuring and using the Azure OpenAI provider in your Code Review project, which leverages Large Language Models (LLMs) to enhance your code quality and prevent bugs before they reach production. + +### Prerequisites + +Before you begin, make sure you have the following: + +- Azure OpenAI service with a model that supports [Structured Outputs](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/structured-outputs) +- Necessary secrets stored in your repository/environment settings. + +To set up the code review script with the Azure OpenAI provider on GitHub CI, add the following configuration in your GitHub Actions workflow file (e.g., .github/workflows/review.yml): + +```yaml +- name: Run code review script + run: bun run start review --ci=github --provider=azureai + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BASE_SHA: ${{ github.event.pull_request.base.sha }} + GITHUB_SHA: ${{ github.sha }} + AZURE_OPENAI_API_INSTANCE_NAME: "my-azure-open-ai-instance" + AZURE_OPENAI_API_DEPLOYMENT_NAME: "gpt-40" + AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} + AZURE_OPENAI_API_VERSION: "2024-08-01-preview" +``` + +Key Environment Variables +AZURE_OPENAI_API_INSTANCE_NAME: The unique name of your Azure OpenAI instance. +AZURE_OPENAI_API_DEPLOYMENT_NAME: The deployment name of the model instance you are utilizing in Azure. +AZURE_OPENAI_API_KEY: Your Azure OpenAI API key; should be stored securely as a secret. +AZURE_OPENAI_API_VERSION: Specifies the API version you're using, supporting future and preview versions. + +### Troubleshooting + +- Invalid API Keys: Double-check your secrets to ensure that they are accurate and up-to-date. +- Deployment Issues: Verify that the deployment names and instance names are correctly stated and match those in your Azure set up. +- `400 Invalid parameter: 'response_format' of type 'json_schema' is not supported with this model` ensure that you're using a model that supports [Structured Outputs](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/structured-outputs) \ No newline at end of file diff --git a/src/args.ts b/src/args.ts index 86d4c933..b5a4420b 100644 --- a/src/args.ts +++ b/src/args.ts @@ -82,7 +82,7 @@ export const getYargs = async (): Promise => { }) .option('provider', { description: 'Provider to use for AI', - choices: ['openai', 'bedrock'], + choices: ['openai', 'azureai', 'bedrock'], type: 'string', default: 'openai', }) diff --git a/src/common/model/AIModel.ts b/src/common/model/AIModel.ts index 010e8a40..7d8b2400 100644 --- a/src/common/model/AIModel.ts +++ b/src/common/model/AIModel.ts @@ -1,4 +1,4 @@ -import { ChatOpenAI } from '@langchain/openai'; +import { ChatOpenAI, AzureChatOpenAI } from '@langchain/openai'; import type { ZodType } from 'zod'; import type { IFeedback } from '../types'; @@ -26,6 +26,11 @@ export class AIModel { modelName: options.modelName, }); break; + case "azureai": + this.model = new AzureChatOpenAI({ + temperature: options.temperature, + }); + break; case 'bedrock': throw new Error('Bedrock provider not implemented'); default: diff --git a/src/config.ts b/src/config.ts index 5b066e9b..52917b31 100644 --- a/src/config.ts +++ b/src/config.ts @@ -9,11 +9,14 @@ In CI: import { logger } from './common/utils/logger'; export const getOpenAIApiKey = (): string => { - if (!process.env.OPENAI_API_KEY) { - logger.error('OPENAI_API_KEY is not set'); + const openAiApiKey = process.env.OPENAI_API_KEY; + const azureOpenAiApiKey = process.env.AZURE_OPENAI_API_KEY; + + if (!openAiApiKey && !azureOpenAiApiKey) { + logger.error('Neither OPENAI_API_KEY nor AZURE_OPENAI_API_KEY is set'); } - return process.env.OPENAI_API_KEY ?? ''; + return openAiApiKey ?? azureOpenAiApiKey ?? ''; }; export const githubToken = (): string => {