groq / groq-typescript

The official Node.js / Typescript library for the Groq API
Apache License 2.0
145 stars 17 forks source link

Typescript build fails #19

Closed dseravalli closed 3 months ago

dseravalli commented 4 months ago

v0.3.2

import Groq from 'groq-sdk';
const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });

async function getGroqCompletion(systemPrompt: string, text: string): Promise<string> {
  const params: Groq.Chat.CompletionCreateParams = {
    messages: [
      { role: "system", content: systemPrompt },
      { role: "user", content: text, } ],
    model: "mixtral-8x7b-32768"
  };
  const completion: Groq.Chat.ChatCompletion = await groq.chat.completions.create(params);
  return completion.choices[0]?.message?.content || '';
}
node_modules/groq-sdk/lib/chat_completions_ext.d.ts:1:32 - error TS2307: Cannot find module 'groq-sdk/resources/chat' or its corresponding type declarations.

1 import { ChatCompletion } from 'groq-sdk/resources/chat';
                                 ~~~~~~~~~~~~~~~~~~~~~~~~~

src/audioRouteHandler.ts:38:27 - error TS2694: Namespace '"redacated/node_modules/groq-sdk/resources/chat/chat".Chat' has no exported member 'CompletionCreateParams'.

38   const params: Groq.Chat.CompletionCreateParams = {
                             ~~~~~~~~~~~~~~~~~~~~~~

Found 2 errors in 2 files.

Running tsc -p tsconfig.json with this config:

{
  "compilerOptions": {
    "strict": true,
    "target": "es2022",
    "module": "node16",
    "lib": [
      "dom",
      "es2022"
    ],
    "moduleResolution": "node16",
    "rootDir": ".",
    "outDir": "build",
    "allowSyntheticDefaultImports": true,
    "importHelpers": true,
    "alwaysStrict": true,
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false
  },
  "include": [
    "src/**/*",
    "__tests__/**/*"
  ]
}
jose-mdz commented 4 months ago

Hey @dseravalli have you tried skipLibCheck?

Your tsconfig.json would look like this:

{
  "compilerOptions": {
    "strict": true,
    "target": "es2022",
    "module": "node16",
    "lib": [
      "dom",
      "es2022"
    ],
    "moduleResolution": "node16",
    "rootDir": ".",
    "outDir": "build",
    "allowSyntheticDefaultImports": true,
    "importHelpers": true,
    "alwaysStrict": true,
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false,
    "skipLibCheck": true
  },
  "include": [
    "src/**/*",
    "__tests__/**/*"
  ]
}
dseravalli commented 4 months ago

@jose-mdz That clears up Cannot find module 'groq-sdk/resources/chat' or its corresponding type declarations. (thank you) but Namespace ... has no exported member 'CompletionCreateParams'. remains.

hozen-groq commented 4 months ago

@dseravalli This question is more of a LangChain question, but one suggestion is the following...

The response in this example is of type:

type(response)
<class 'langchain_core.messages.ai.AIMessage'>

And the content can be extracted via: response.content

You could try converting it into a string and then trying to extract content from it!

dseravalli commented 4 months ago

@dseravalli This question is more of a LangChain question, but one suggestion is the following...

The response in this example is of type:

type(response)
<class 'langchain_core.messages.ai.AIMessage'>

And the content can be extracted via: response.content

You could try converting it into a string and then trying to extract content from it!

I'll give it a shot. FYI this code I'm using is pulled directly from the README.

gamer-mitsuha commented 4 months ago

Hey @dseravalli have you tried skipLibCheck?

Your tsconfig.json would look like this:

{
  "compilerOptions": {
    "strict": true,
    "target": "es2022",
    "module": "node16",
    "lib": [
      "dom",
      "es2022"
    ],
    "moduleResolution": "node16",
    "rootDir": ".",
    "outDir": "build",
    "allowSyntheticDefaultImports": true,
    "importHelpers": true,
    "alwaysStrict": true,
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false,
    "skipLibCheck": true
  },
  "include": [
    "src/**/*",
    "__tests__/**/*"
  ]
}

Instead of skipping the lib check, I think it's better to fix the error in the code.

FYI, I tried changing import { ChatCompletion } from 'groq-sdk/resources/chat/'; in groq-sdk/lib/chat_completions_ext.d.ts to import { ChatCompletion } from 'groq-sdk/resources/chat/index';`, and after this change, the TS compilation succeeded.

gradenr commented 4 months ago

A fix for this issue has been publish in release v0.3.3.

Thanks for the fix @gamer-mitsuha