token-js / token.js

Integrate 200+ LLMs with one TypeScript SDK using OpenAI's format.
https://docs.tokenjs.ai/
MIT License
69 stars 5 forks source link

Resolve TypeScript type error that occurs when defining a variable of type `ChatCompletionMessageParam` #67

Closed sam-goldman closed 2 months ago

sam-goldman commented 2 months ago

Problem

Users cannot define a variable of type ChatCompletionMessageParam without causing a TypeScript type error, even when the variable has valid fields according to this type.

Steps to Reproduce

Copy and paste the following into a file:

import { ChatCompletionMessageParam, TokenJS } from './index'

// Create the Token.js client
const tokenjs = new TokenJS()

const main = async () => {
  const messages: ChatCompletionMessageParam = [
    {
      role: 'user',
      content: 'Hello!',
    },
  ]
  // Create a model response
  const completion = await tokenjs.chat.completions.create({
    // Specify the provider and model
    provider: 'openai',
    model: 'gpt-4o',
    // Define your message
    messages,
  })
  console.log(completion.choices[0])
}
main()

Expected Result

No type error should occur for the messages variable

Actual Result

Screenshot 2024-07-13 at 3 42 12 PM
sam-goldman commented 2 months ago

Closed because this was a mistake on my part. It should be const messages: Array<ChatCompletionMessageParam> (note the Array