Can't get it working with telegraf

const openai = require('openai');
const { openaiApiKey, telegramToken } = require('../creds/config');

// Set up OpenAI API key
openai.apiKey = openaiApiKey;

// Set up bot
const bot = new Telegraf(telegramToken);

// Handle incoming messages
bot.on('message', async (ctx) => {
  // Only respond to text messages
  if (ctx.message.text) {
    // Generate response using OpenAI
    try {
      const response = await openai.completions.create({
        engine: 'davinci',
        prompt: ctx.message.text,
        maxTokens: 50,
        n: 1,
        stop: '\n'

      // Send response to user
    } catch (err) {
      ctx.reply('An error occurred while generating a response.');

// Start bot

I'm trying to use chatgpt to make me a node.js app that allows me to chat with it.
I wanted it to use the userid to allow for separate conversations for each user, but no openai modules that chatgpt ever make work for me. 
Can someone help change this into something that allows users to chat using the chatgpt api?

The code gives this error:

    at /root/Shonin/playground/test.js:17:49
    at /root/Shonin/node_modules/telegraf/lib/composer.js:167:111
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async execute (/root/Shonin/node_modules/telegraf/lib/composer.js:474:17)
    at async /root/Shonin/node_modules/telegraf/lib/composer.js:475:21
    at async execute (/root/Shonin/node_modules/telegraf/lib/composer.js:474:17)
    at async /root/Shonin/node_modules/p-timeout/index.js:50:13```

The above will not work. as you need to use the model param and not the engine param, @seanfox1989

You seem to be working with a deprecated example.

Don’t forget to review the OpenAI API docs. While many people do not like reading the docs, the docs are very helpful and more up-to-date than older code out there on the internet (and will save you a lot of time and energy).


I’m only just learning how to code with the help of GPT3, but clearly not having much luck :')

I changed the engine to model like so - model: 'gpt-3.5-turbo',

It’s just the error about the TypeError: Cannot read properties of undefined (reading 'create')

The only thing I can get back from chatgpt is that my keys are wrong, but I suspect that it’s just writing based on an old version - as hardcoding the keys to test results in the same error. Super frustrating.

Yes, be careful trying to write code using ChatGPT or any LLM.

Non-coders get all excited about it, thinking they can become overnight coders, but the “real” coders realize how error prone the models are.

You cannot depend on these LLM models to write good, clean code and you must verify every line.

In addition, you are using a model which is getting close to 2 years old (the underlying data) and code deprecates very fast in the real world of tech.



Just sucks that everything seems to be python centric in the docs.
It’s just the new chatgpt API that I want to look into, but I’m doing a JavaScript course and it’s pretty intense as it is without trying to learn a new language

Yeah, if you do not have prior programming experience, starting off learning Javascript is a hard way to go.

I write a lot of code, and for many decades, and I avoid Javascript (personally, nothing wrong with it), so you have really started off down a very tough path.

Good luck!


1 Like