I have spent some time reading over other threads but not finding what I need to get started. If I have missed answer here pot OpenAI API docs, please forgive this poor eye-sight gal:)
When I call openai with my example.php, there is a response returned from openai api that under dev network / Response. example below.
Not sure if this is allowed in the forums, but this actually seemed like a really good question for GPT4 to answer?
To extract and display the data from the response you received from the OpenAI API on a webpage using PHP and JavaScript, you can follow these steps:
PHP Backend
Receive the API Response: Assuming you’re using PHP to call the OpenAI API, you would receive the JSON response as a string.
Decode the JSON Response: Use json_decode in PHP to convert the JSON string into an associative array.
Extract the Required Data: Access the usage field of the array to get prompt_tokens, completion_tokens, and total_tokens.
Send the Data to the Frontend: You can either render this data directly in PHP or send it to the frontend (JavaScript) through an AJAX call.
Example PHP Code:
<?php
// Example response from OpenAI API
$responseFromOpenAI = '{"id": "chatcmpl-XXXXXXXXXXXXXXXXXXXX", "object": "chat.completion", "created": 1702400270, "model": "gpt-4-1106-vision-preview", "usage": {"prompt_tokens": 820, "completion_tokens": 520, "total_tokens": 1340}}';
// Decoding the JSON string to an associative array
$responseArray = json_decode($responseFromOpenAI, true);
// Extracting usage data
$usageData = $responseArray['usage'];
// You can echo these values directly in PHP or pass them to JavaScript
echo json_encode($usageData);
?>
JavaScript Frontend
Fetch the Data: Use AJAX (with XMLHttpRequest or fetch API) to get the data from your PHP backend.
Display the Data: Once you receive the data, you can display it on the webpage using DOM manipulation.
Example JavaScript Code:
fetch('path_to_your_php_script.php')
.then(response => response.json())
.then(data => {
// Assuming you have HTML elements with these IDs
document.getElementById('promptTokens').textContent = 'Prompt Tokens: ' + data.prompt_tokens;
document.getElementById('completionTokens').textContent = 'Completion Tokens: ' + data.completion_tokens;
document.getElementById('totalTokens').textContent = 'Total Tokens: ' + data.total_tokens;
})
.catch(error => console.error('Error:', error));
This is a basic example. Depending on your application’s architecture, you might need to adjust the AJAX call, the PHP script’s location, and how you handle the response.
The reply was hidden because it was flagged for being a bot-generated answer without disclosure, little relevance to the topic, or value to the community.
If you are not using streaming (where you don’t get a report, but rather have to encode the text received word-by-word to tokens yourself and then count with a library like tiktoken), but instead are just using the completion normally and getting the statistics for “usage” as you show, you’d need to parse the return object, loading json into arrays or using a json method, or even simply doing string matching.
When I ask an AI, knowing what you are looking at and looking for, I get a more cohesive answer:
// Decode the JSON string into a PHP array
$data = json_decode($jsonString, true);
// Access the value of "total_tokens"
$totalTokens = $data['usage']['total_tokens'];
I have not gone through the responses here and responding based on the Subject. I have the following code in Python that goes thru resumes (in PDF format) in a folder and extracts key information and writes to an excel file - so this function is called within a loop:
Function to extract relevant information from text using OpenAI API
async def extract_info_async(text):
try:
chat_completion = await client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Prompt goes here:" + text,
}
],
model="gpt-3.5-turbo",
max_tokens=3000,
temperature=0.7,
stop=["Generated content:"],
)
# Access the generated content from the response
generated_content = chat_completion.choices[0].message.content.strip()
# Ensure there are no spaces after labels like "Skills:"
generated_content = re.sub(r'(\bSkills:)\s+', r'\1', generated_content)
# Ensure there are no spaces after labels like "Qualifications:"
generated_content = re.sub(r'(\bQualifications:)\s+', r'\1', generated_content)
# Access total tokens from the usage dictionary
tokens_used = 0
tokens_used += chat_completion.usage.total_tokens
global total_tokens_used
total_tokens_used += tokens_used
print()
print("Tokens Used for this Bulk Resume Job: ", tokens_used)
print()
return generated_content
except Exception as e:
print(f"Error during API call: {e}")
return ""
At the end of the script, I have the following code to print he Total Tokens Used:
Define the cost per million tokens
cost_per_million_tokens = 1.50 # USD
# Function to calculate the cost for a given number of tokens
def calculate_cost(total_tokens_used):
# Calculate the cost based on the number of tokens used
cost = (total_tokens_used / 1_000_000) * cost_per_million_tokens
return cost
# Get cost for the total tokens used
cost = calculate_cost(total_tokens_used)
print(f"Cost for {total_tokens_used} tokens: ${cost:.2f}")