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

Bug fix on azure open ai #164

Merged
merged 2 commits into from
Nov 8, 2024
Merged

Bug fix on azure open ai #164

merged 2 commits into from
Nov 8, 2024

Conversation

shkangomelet
Copy link
Contributor

@shkangomelet shkangomelet commented Nov 8, 2024

I apologize, but it turns out that my recent commit (#163) isn’t functioning as expected. Unfortunately, I created it manually without thoroughly testing.

Now, after testing with the following script, I have confirmed it works well:

import { OpenAI, AzureOpenAI } from 'openai';

export class Chat {
  private openai: OpenAI | AzureOpenAI;
  private isAzure: boolean;

  constructor(apikey: string) {
    this.isAzure = Boolean(
        process.env.AZURE_API_VERSION && process.env.AZURE_DEPLOYMENT,
    );

    if (this.isAzure) {
      // Azure OpenAI configuration
      this.openai = new AzureOpenAI({
        apiKey: apikey,
        endpoint: process.env.OPENAI_API_ENDPOINT || '',
        apiVersion: process.env.AZURE_API_VERSION || '',
        deployment: process.env.AZURE_DEPLOYMENT || '',
      });
    } else {
      // Standard OpenAI configuration
      this.openai = new OpenAI({
        apiKey: apikey,
        baseURL: process.env.OPENAI_API_ENDPOINT || 'https://api.openai.com/v1',
      });
    }
  }

  private generatePrompt = (patch: string) => {
    const answerLanguage = process.env.LANGUAGE
        ? `Answer me in ${process.env.LANGUAGE},`
        : '';

    const prompt =
        process.env.PROMPT ||
        'Below is a code patch, please help me do a brief code review on it. Any bug risks and/or improvement suggestions are welcome:';

    return `${prompt}, ${answerLanguage}:
  ${patch}
    `;
  };

  public codeReview = async (patch: string) => {
    if (!patch) {
      return '';
    }

    console.time('code-review cost');
    const prompt = this.generatePrompt(patch);

    const res = await this.openai.chat.completions.create({
      messages: [
        {
          role: 'user',
          content: prompt,
        },
      ],
      model: process.env.MODEL || 'gpt-4o-mini',
      temperature: +(process.env.temperature || 0) || 1,
      top_p: +(process.env.top_p || 0) || 1,
      max_tokens: process.env.max_tokens ? +process.env.max_tokens : undefined,
    });

    console.timeEnd('code-review cost');

    if (res.choices.length) {
      return res.choices[0].message.content;
    }

    return '';
  };
}

// Test code to run codeReview directly
async function testCodeReview() {
  const apiKey = process.env.OPENAI_API_KEY || '';
  const chat = new Chat(apiKey);

  // Sample code patch for review
  const patch = `
  function add(a, b) {
    return a + b;
  }
  `;

  try {
    const review = await chat.codeReview(patch);
    console.log('Code Review Output:', review);
  } catch (error) {
    console.error('Error in codeReview:', error);
  }
}

// Run the test
testCodeReview();

I’ve tested the script using the following two configurations, and it performed as expected:

export OPENAI_API_KEY="azure_api_key"                 
export OPENAI_API_ENDPOINT="https://omelet-openai-us.openai.azure.com"
export AZURE_API_VERSION="2024-08-01-preview"
export AZURE_DEPLOYMENT="gpt-4o-2"
export MODEL="gpt-4o"
export OPENAI_API_KEY="openai_api_key"                 
export OPENAI_API_ENDPOINT=""
export AZURE_API_VERSION=""
export AZURE_DEPLOYMENT=""
export MODEL="gpt-4o"

reference: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/README.md

@shkangomelet
Copy link
Contributor Author

image
image
I added two test result

@anc95
Copy link
Owner

anc95 commented Nov 8, 2024

@shkangomelet could you run npm run build locally and then commit and push the update.

@shkangomelet
Copy link
Contributor Author

@anc95 I did it!

@anc95 anc95 merged commit 355a405 into anc95:main Nov 8, 2024
@anc95
Copy link
Owner

anc95 commented Nov 8, 2024

released. Thank you 👍

@shkangomelet
Copy link
Contributor Author

image
@anc95 Now it's working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants