Update Modul OpenAI, because not working

Please update modul OpenAI , thanks u so much. im new developer in openAI please help me. modul python and node js. thanks u

Welcome to the dev forum!

What are you trying to do exactly?

Are you having problems with code? Can you share?

Looking for a no-code solution?

import asyncio
from telethon import TelegramClient, events
import openai
import base64
import logging
import os

# Set up your API ID and API Hash
api_id = 'xxxxxxxxxxxxxxxx'
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
session_name = 'xxxxxxxxxxxxxxxxxx'
openai_api_key = 'xxxxxxxxxxxxxxxxx'

# Initialize the client
client = TelegramClient(session_name, api_id, api_hash)

# Set up logging
logging.basicConfig(level=logging.INFO)

# Set up OpenAI API key
openai.api_key = openai_api_key

def fix_base64_padding(base64_str):
    return base64_str + '=' * (4 - len(base64_str) % 4)

async def handle_new_message(event):
    logging.info('Received a new message')
    
    if event.photo:
        logging.info('Message contains a photo')
        
        # Download the photo
        photo = await event.download_media()
        logging.info(f'Photo downloaded: {photo}')
        
        # Convert the downloaded image to base64
        with open(photo, "rb") as image_file:
            base64_image = base64.b64encode(image_file.read()).decode('utf-8')
        
        logging.info('Photo converted to base64')
        
        # Prepare the request payload for OpenAI
        request_body = {
            "model": "gpt-4",
            "messages": [
                {
                    "role": "system",
                    "content": "Jawaban Singkat saja. gabungkan menjadi satu kata."
                },
                {
                    "role": "user",
                    "content": f"data:image/jpeg;base64,{base64_image}"
                }
            ],
            "temperature": 0.7,  # Use a lower temperature for more deterministic results
            "max_tokens": 150,   # Reduce max tokens to ensure concise response
            "top_p": 0.9,
            "frequency_penalty": 0,
            "presence_penalty": 0
        }

        # Make the API request to OpenAI
        logging.info('Sending request to OpenAI API')
        try:
            response = openai.ChatCompletion.create(**request_body)
            
            content = response.choices[0].message['content']
            logging.info(f'OpenAI response content: {content}')
            
            # Fix base64 padding if necessary
            fixed_content = fix_base64_padding(content)

            try:
                decoded_bytes = base64.b64decode(fixed_content)
                decoded_text = decoded_bytes.decode('utf-8')
                logging.info(f'Decoded OpenAI response content: {decoded_text}')
                
                # Send the decoded text back to the user in Telegram
                await event.reply(decoded_text)
                logging.info('Response sent to Telegram user')
            except Exception as e:
                logging.error(f'Error decoding base64 content: {e}')
                await event.reply('Received an invalid response format from OpenAI.')
                logging.info('Sent error message to Telegram user')
        
        except Exception as e:
            logging.error(f'Error from OpenAI API: {e}')

# Set up event handler for new messages
@client.on(events.NewMessage)
async def new_message_listener(event):
    await handle_new_message(event)

# Start the Telegram client
async def main():
    await client.start()
    logging.info("Client is running...")
    await client.run_until_disconnected()

asyncio.run(main())
```  C:\Users\Administrator\Desktop\Server>python Ai.py
INFO:telethon.network.mtprotosender:Connecting to 91.108.56.104:443/TcpFull...
INFO:telethon.network.mtprotosender:Connection to 91.108.56.104:443/TcpFull comp
lete!
INFO:telethon.network.mtprotosender:System clock is wrong, set time offset to -2
5200s
WARNING:telethon.network.mtprotostate:Server resent the older message 7377566976
425809921, ignoring
INFO:root:Client is running...
INFO:root:Received a new message
INFO:root:Message contains a photo
INFO:telethon.client.downloads:Starting direct file download in chunks of 131072
 at 0, stride 131072
INFO:root:Photo downloaded: photo_2024-06-07_01-27-59.jpg
INFO:root:Photo converted to base64
INFO:root:Sending request to OpenAI API
ERROR:root:Error from OpenAI API:

You tried to access openai.ChatCompletion, but this is no longer supported in op
enai>=1.0.0 - see the README at https://github.com/openai/openai-python for the
API.

You can run `openai migrate` to automatically upgrade your codebase to use the 1
.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. `pip insta
ll openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-p
ython/discussions/742

Okay. Are you getting an error?

I’m still not sure I understand your question. Sorry!

C:\Users\Administrator\Desktop\Server>python Ai.py
INFO:telethon.network.mtprotosender:Connecting to 91.108.56.104:443/TcpFull…
INFO:telethon.network.mtprotosender:Connection to 91.108.56.104:443/TcpFull comp
lete!
INFO:telethon.network.mtprotosender:System clock is wrong, set time offset to -2
5200s
WARNING:telethon.network.mtprotostate:Server resent the older message 7377566976
425809921, ignoring
INFO:root:Client is running…
INFO:root:Received a new message
INFO:root:Message contains a photo
INFO:telethon.client.downloads:Starting direct file download in chunks of 131072
at 0, stride 131072
INFO:root:Photo downloaded: photo_2024-06-07_01-27-59.jpg
INFO:root:Photo converted to base64
INFO:root:Sending request to OpenAI API
ERROR:root:Error from OpenAI API:

You tried to access openai.ChatCompletion, but this is no longer supported in op
enai>=1.0.0 - see the README at

You can run openai migrate to automatically upgrade your codebase to use the 1
.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. pip insta ll openai==0.28

A detailed migration guide is available

so my goal is, i want to read picture messages in telegram with openai. but I can’t read picture messages from telegram with ai open. please help sir.

Great. We’re making progress… :slight_smile:

ERROR:root:Error from OpenAI API:

You tried to access openai.ChatCompletion, but this is no longer supported in op
enai>=1.0.0 - see the README at

You can run openai migrate to automatically upgrade your codebase to use the 1
.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. pip insta ll openai==0.28

A detailed migration guide is available

Have you done any of this yet?

If you’re not very technical, you might have to hire someone to set it up for you. Basically, though, you can install the old library (not recommended) or change your code to work with the new library… or find new code on github.

So basically I couldn’t use the official Openai documentation, I was told to look for modules made by other people. Not official from Openai. is that what you mean?

Well, you’re using an old script for your Telegram bot. Might be best to update that to a more recent version and go from there…

Your OpenAI Python library is probably updated but the code you found is wrong.

okay, thank you very much.can you read telegram messages images and videos with openai api??