Portkey-AI / gateway

A Blazing Fast AI Gateway. Route to 200+ LLMs with 1 fast & friendly API.
https://portkey.ai/features/ai-gateway
MIT License
5.21k stars 356 forks source link

Bug : Incorrect Variable Name in README Code Example #384

Closed Arindam200 closed 6 days ago

Arindam200 commented 1 month ago

What Happened?

There is a mistake in the code example provided in the README file. The code snippet uses await portkey.chat but it should be await gateway.chat as defined in the const declaration. There is no variable named portkey in the provided code.

Code Snippet with Issue:

import OpenAI from 'openai';
import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'

const gateway = new OpenAI({
   apiKey: "ANTHROPIC_API_KEY",
    baseURL: PORTKEY_GATEWAY_URL, // Or http://localhost:8787/v1 when running locally
    defaultHeaders: createHeaders({
        provider: "anthropic",
        apiKey: "PORTKEY_API_KEY" // Grab from https://app.portkey.ai / Not needed when running locally
  })
});

async function main(){
   const chatCompletion = await portkey.chat.completions.create({
      messages: [{ role: 'user', content: 'Who are you?' }],
      model: 'claude-3-sonnet-20240229',
      maxTokens:512
   });
   console.log(chatCompletion.choices[0].message.content);
}

main()

Suggested Fix:

The variable portkey should be replaced with gateway in the await statement.

Corrected Code Snippet:

import OpenAI from 'openai';
import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'

const gateway = new OpenAI({
   apiKey: "ANTHROPIC_API_KEY",
    baseURL: PORTKEY_GATEWAY_URL, // Or http://localhost:8787/v1 when running locally
    defaultHeaders: createHeaders({
        provider: "anthropic",
        apiKey: "PORTKEY_API_KEY" // Grab from https://app.portkey.ai / Not needed when running locally
  })
});

async function main(){
   const chatCompletion = await gateway.chat.completions.create({
      messages: [{ role: 'user', content: 'Who are you?' }],
      model: 'claude-3-sonnet-20240229',
      maxTokens:512
   });
   console.log(chatCompletion.choices[0].message.content);
}

main()

Steps to Reproduce:

  1. Copy the code snippet from the README.
  2. Attempt to run the code.
  3. Observe the error due to the undefined variable portkey.

Expected Behavior:

The code should run without errors and produce the expected output.

Actual Behavior:

The code throws an error because portkey is not defined.

What Should Have Happened?

The code should run without errors and produce the expected output.

Relevant Code Snippet

import OpenAI from 'openai';
import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'

const gateway = new OpenAI({
   apiKey: "ANTHROPIC_API_KEY",
    baseURL: PORTKEY_GATEWAY_URL, // Or http://localhost:8787/v1 when running locally
    defaultHeaders: createHeaders({
        provider: "anthropic",
        apiKey: "PORTKEY_API_KEY" // Grab from https://app.portkey.ai / Not needed when running locally
  })
});

async function main(){
   const chatCompletion = await portkey.chat.completions.create({
      messages: [{ role: 'user', content: 'Who are you?' }],
      model: 'claude-3-sonnet-20240229',
      maxTokens:512
   });
   console.log(chatCompletion.choices[0].message.content);
}

main()

Your Twitter/LinkedIn

https://x.com/Arindam_1729 and https://www.linkedin.com/in/arindam2004/

Arindam200 commented 1 month ago

I want to work on this

vrushankportkey commented 1 month ago

Thanks so much, @Arindam200! Raised a PR for this - https://github.com/Portkey-AI/gateway/pull/390