How to make ChatGPT not only write code, but also execute it

I continue this series of articles: the first and second.

Here I will give a minimalistic example of how to make a conditional ChatGPT (or Llama or any other model) not only write code but also execute it. At the output we will get a bot with which you can communicate as with a regular ChatGPT and set tasks that require complex calculations.

An example of a dialogue with a bot (its code is below…)

For clarity, the code generated by the model for the response has been added to each message.

Let's write such a bot

We'll take this Telegram bot as the shell in which everything will happen and modify its code a little.

The main idea behind the solution is to make the language model write Python code for each answer. It should always do this, even if the answer does not require calculations. This way we can avoid the system message and reduce glitches.

In order for the model not to be able to avoid writing code, we will pass only one tool to its input and write down the condition tool_choice="required".

In the tool we will explain in detail to the model what it should do:

tools = [
    {"type": "function",
     "function": {
         "name": "execute_Python_code",
         "description": "A tool that executes python code. The result of the code execution will be sent to the user as your response. Use this tool for all responses, including text ones. Example: print('Hello! How can I assist you today?')",
         "strict": True,
         "parameters": {
             "type": "object",
             "properties": {
                 "python_code": {
                     "type": "string",
                     "description": "Python code. To receive outputs, they must be printed with print() method."}},
             "required": ["python_code"],
             "additionalProperties": False}}}]

We will run the code created by the model using the following function:

def execute_Python_code(code):
    output = io.StringIO()
    try:
        with contextlib.redirect_stdout(output):
            exec(code, globals())
        return output.getvalue()
    except Exception as e:
        return e

And in the Telegram bot we will add a call to the tool and in each message we will add the code created by the model for clarity:

# pip install pyTelegramBotAPI openai
import telebot, json, contextlib, io
from openai import OpenAI

client = OpenAI(api_key='OpenAI_API_KEY')
bot = telebot.TeleBot("TELEGRAM_BOT_TOKEN")
messages = []

# tools = [...] сюда копируем tools и функцию для выполнения кода
# def execute_Python_code(code): ...

@ bot.message_handler(content_types=['text'])
def get_text_messages(message):
    global messages
    messages.append({"role": 'user', "content": message.text})
    if len(messages) > 6:
        messages = messages[-6:]
    # Указываем инструмент
    response = client.chat.completions.create(model="gpt-4o-mini", messages=messages, tools=tools, tool_choice="required", temperature=0)
    # Вынимаем из ответа Python code
    tool_call = response.choices[0].message.tool_calls[0]
    code = json.loads(tool_call.function.arguments)['python_code']
    # Исполняем код и ответ отправляем
    reply = execute_Python_code(code)
    bot.send_message(message.from_user.id, reply)
    messages.append({"role": 'assistant', "content": f"{reply}\n<pre>{code}</pre>"}, parse_mode="HTML")

while True:
    bot.polling(none_stop=True, interval=0, timeout=0)

Paste into code tools and function execute_Python_code and replace in the code OpenAI_API_KEY on the api key received from OpenAI and TELEGRAM_BOT_TOKEN to the telegram bot token, everything should be in quotes.

Example of work

I made a bot OurGPThe also writes code for each message.

More OurGPT can create pictures, recognize image content, search the internet, understand voice messages and he is very smart))

P.S. It won't ping your IP as in the example, it's not allowed to do that.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *