If you have used chatGPT to try and create code for use with OpenAI endpoints, you probably know it can’t reliably generate working code for all endpoints because its training is outdated.
I created a custom GPT that can. It’s currently the only one in the GPT store that can as far as I’m aware.
Check it out by searching OpenAIAPI Guide by GuideGPTs
It’s by no means perfect, but I plan to continuously refine it if there is interest.
For the custom GPT builders interested in how I did it, I have a thread outlining it on my X profile, @mlesterWS. Find it in the highlights section.
Cannot code. I write everything for the AI except to get the response out. Can’t write code to get a response from chat completions.
Try 1: The thing is unaware of the pydantic objects, immediately erroring.
Trying to improve on the error, it says the API call with streaming must not be streaming and spits out non-functional boilerplate.
Try 2: Same input code as the chat share, simplified, just needing 10 more lines to be done.
It invokes code interpreter. We get a stupid requests library code that cannot work. And a “do it yourself”.
Based on the OpenAI API specification, to complete the code for parsing the streamed response from a chat completion request, you can use the example provided for creating a chat completion. Here is a continuation of the provided Python code snippet, focusing on parsing the chunks received as a stream response:
import requests
import json
# Make the API call to OpenAI
response = requests.post(
'https://api.openai.com/v1/chat/completions',
headers={
'Authorization': f'Bearer YOUR_OPENAI_API_KEY',
'Content-Type': 'application/json'
},
json=params,
stream=True
)
# Parse the streamed response
for line in response.iter_lines():
if line: # filter out keep-alive new lines
decoded_line = line.decode('utf-8')
json_line = json.loads(decoded_line)
# Check for http headers starting with 'x' and print them alphabetically
if 'x' in json_line: # Assuming 'x' will be in the json keys for headers
x_headers = {k: v for k, v in json_line.items() if k.startswith('x')}
for key in sorted(x_headers):
print(f'{key}: {x_headers[key]}')
# Print the token chunks of AI response content as they are received
if 'choices' in json_line and 'message' in json_line['choices'][0]:
print(json_line['choices'][0]['message']['content'])
# Print any tool call or function call objects after they are reassembled
if 'tool_calls' in json_line:
print(json_line['tool_calls'])
# Note: Replace 'YOUR_OPENAI_API_KEY' with your actual OpenAI API key.
This code snippet demonstrates how to parse each line of the streamed response from the OpenAI API. It filters out keep-alive new lines and decodes each line to JSON. It then prints headers starting with ‘x’, the content of AI responses as they are received, and any tool or function call objects. Remember to replace 'YOUR_OPENAI_API_KEY' with your actual OpenAI API key.
So I will call the claims exaggerated, and the post, self-promotion of some GPT guide site.
First, note that I did say it is not perfect by any means.
Blockquote
Cannot code. I write everything for the AI except to get the response out. Can’t write code to get a response from chat completions.
Yeah, it’s not gonna be able to process that much code in one shot. I’m skeptical that any GPT could. But if you broke it down into steps it would do a better job of helping you. If you know of a GPT that can do this, I’d be very interested in seeing it. I ran it through Grimoire and even it couldn’t.
Blockquote
The thing is unaware of the pydantic objects
You’re right it struggles with creating code to parse objects. I am working on fixing that.
Perhaps I should’ve been more clear, but the point of the GPT with its current limitations is to create correct endpoint segments of code and help you integrate them. It doesn’t have 100% accuracy, but from my testing it has higher accuracy than other GPTs. Other GPTs typically fail with chat/completions and completions/.
Again, thanks for the feedback though, I’ll use this to try and improve the GPT.
I’ve had a chance to look more closely at this, and it looks like you’ll have to wait for AGI to get help with your code…
Lol. But seriously as far as I can tell your code is very idiosyncratic. Which is fine, you’re obviously an excellent coder, but definitely not the target audience.
Your feedback prompted me to update the GPT to be able to give code for streaming though, so thanks again.
For anyone else reading through this, please don’t be deterred by _j’s experience. For beginner and intermediates to OpenAI API, this GPT will likely help you.