The original poster from 14 months ago has probably gone on to form their own AI startup or long ago given up. This is what we call a mega-bump.
Some turns of having a programming GPT in ChatGPT Plus make the desired app, where all the work was fighting its pretraining to use the exact methods provided in prompt and also part of its instructions and also file knowledge.
The length I had set the max_tokens. The tone is in a “response style/behavior” container, so one could say “short responses only” to instruct the AI to do so. But making it understand how to use the API was simply multi-shot to the AI answering the OP question for my own amusement, which is ultimately just repeating back my input:
Hello!
It sounds like you’re on the right track, and I’m happy to help explain how to integrate multiple input fields into a request for the OpenAI API.
How the API Works
The OpenAI API provides an endpoint client.chat.completions.with_raw_response.create()
that accepts a list of messages in the format [{ "role": "system" | "user" | "assistant", "content": "..." }]
. The API returns a generator object which contains a streaming response, and you can iterate over each chunk to process the result.
Specifying Parameters
-
Messages: The messages
parameter is a list combining inputs like prompts and system instructions.
- System: Provides a context or persona to the assistant.
- User: Contains the user prompt.
-
Model: Specify the language model (e.g., "gpt-3.5-turbo-0125"
).
-
Max Tokens: Controls the length of the response (e.g., max_tokens=100
).
-
Temperature & Top P:
- Temperature: Controls randomness (e.g.,
temperature=0.5
).
- Top P: Alternative sampling method.
-
Streaming: Enable stream=True
for word-by-word responses.
-
Stream Options: You can include additional options like {"include_usage": True}
.
Sending the Request
Given a form with fields for the prompt, tone, and length, you’d follow these steps:
-
Combine Inputs:
- Prompt: User input.
- Tone: Additional system instruction.
- Length:
max_tokens
parameter.
-
Build the Messages List:
- Create a system message (persona or tone context).
- Add user prompts and previous chat history.
-
Make the API Request:
- Call
client.chat.completions.with_raw_response.create()
with the specified parameters.
Parsing the Response
The response is a generator object containing chunks of streaming data. Each chunk represents a segment of the conversation, allowing for real-time updates to the application.
Here’s how you handle it:
-
Iterate Over the Response:
- Use a
for
loop to process each chunk.
- Extract
choices
and usage
data.
-
Build the Full Response:
- Concatenate all text chunks to form the complete response.
-
Add to Chat History:
- Append user prompts and assistant responses for subsequent context.
Example Explanation
Below is a step-by-step explanation based on the working code:
-
Initialize OpenAI Client:
client = openai.Client()
-
Build System and Chat Messages:
system = [{"role": "system", "content": "You are ChatExpert, a large language model AI assistant."}]
chat = [...] # Your chat history
user_prompt = [{"role": "user", "content": "Prompt content"}]
messages = system + chat[-10:] + user_prompt
-
Make the Request:
response = client.chat.completions.with_raw_response.create(
messages=messages,
model="gpt-3.5-turbo-0125",
max_tokens=100,
temperature=0.5,
top_p=0.5,
stream=True,
stream_options={"include_usage": True}
)
-
Parse the Response:
content = ""
for chunk in response.parse():
if chunk.choices:
if not chunk.choices[0].finish_reason:
word = chunk.choices[0].delta.content or ""
content += word
else:
finish_reason = chunk.choices[0].finish_reason
if chunk.usage:
usage_dict = chunk.usage
-
Update Chat History:
chat.append({"role": "user", "content": "Prompt content"})
chat.append({"role": "assistant", "content": content})
Conclusion
In summary, you combine different inputs by structuring them in the messages
parameter and adjusting the other options like max_tokens
and temperature
. You then process the streaming response chunk by chunk to build the full response.
I hope this explanation helps clarify things for you. Feel free to ask any further questions.
Thanks!
It also describes streaming features introduced yesterday…