How to pass large strings in gpt-3.5 turbo

Hello, I am trying to setup a gpt-3.5 turbo chat API in my python program and I have a large string with some code that I want the gpt to interpret for me. However, the string is in a variable and I don’t know how to pass the string to the gpt model. Can I just put the variable name in the request and the API call will parse it for me or is there another way to accomplish it?

Heya. Welcome to the dev forum.

Yes, that’s basically how it works.

Do you have a code sample you can share? Or let us know if you run into problems.

2 Likes

Hey. Thank you for your reply.

I got the code working, however the API is raising an error saying that the variable is too large for the input. I broke the input down into a list but I am unsure how to pass that list in the API call. Can you provide any help in this matter?

What model are you using? Some of them have a larger “context window”…

You can find out more here…

https://platform.openai.com/docs/models/model-endpoint-compatibility

1 Like

I am using gpt-3.5-turbo. I looked at the link and found out that none of the models have large enough context window that can fit my use case. Is there a way to continuously feed the input and get the output?

No, you’ll need to chunk it (break it up) on your end on your server then feed it a little at a time as the context window allows.

What are you trying to do? Maybe there’s a better way?

What I am trying to achieve is to pass in assembly code and ask GPT to convert it to readable C code. This is for my research project and I am testing how effective GPT models are in converting assembly to C that is easy to read and understand.

Ah, gotcha. There’s a few models trained on code that might be better.

That said, try with just a few functions / classes maybe rather than whole projects?

That’s the thing. I am using a custom disassembler that parses the main code sections of the assembly code. They are not able to differentiate between code sections and thus, they output the entire program in assembly. But, I’ll figure out a way.

Can you tell me which models are trained on code that might be appropriate for my use case?

I believe CoPilot is the biggest/latest? There might be a Meta model too?

I’m doubting it’ll do very well going from assembly to C of any flavor, though. :wink:

ChatGPT (and its version of gpt-4-turbo) seems to give more replication of possible object code than GPT-4 providing a printf on some hello world NASM.

gpt-3.5-turbo can’t even do long Python well; exploring the limits of AI capabilities is going to take GPT-4.

gpt-4-turbo models have an input context of up to 125k tokens, however, that will be in excess of what the AI can actually produce as translated output of 4k maximum response.

In Python:

from openai import OpenAI
client = OpenAI()

# use a multi-line docstring to make paste into code easy
mycode = """
nop   ; does nothing
""".strip()

response = client.chat.completions.create(
    model="gpt-4-1106-preview",
    messages=[
        {
            "role": "system",
            "content": "You are an expert programmer, helpfully assisting in complicated programming tasks."
        },
        {
            "role": "user",
            "content": "Convert this NASM assembly code to C programming language as closely as possible with the goal of producing similar object code.\n\ncode:\n" + mycode
        }
    ],
    max_tokens=2000,
    top_p=0.1,
)

# Extracting the text from the Pydantic response object
completion_text = response.choices[0].message.content
print(completion_text)
1 Like