nasirus / llama_index

MIT License
1 stars 0 forks source link

How to get the usage and other information responded by OpenAI? #6

Open nasirus opened 1 year ago

nasirus commented 1 year ago

This is the response message from the OpenAI api:

{ "id":"chatcmpl-abc123", "object":"chat.completion", "created":1677858242, "model":"gpt-3.5-turbo-0301", "usage":{ "prompt_tokens":13, "completion_tokens":7, "total_tokens":20 }, "choices":[ { "message":{ "role":"assistant", "content":"\n\nThis is a test!" }, "finish_reason":"stop", "index":0 } ] } Here's the response I get using index.query:

{ "extra_info": null, "response": "text", "source_nodes": [ { "node": { "doc_hash": "42b5f8b6acb0c03e229b6093479990dccf70e77525d9ce869cabea7bc8389460", "doc_id": "047322ad-3bc6-49be-a2fb-6a0a710c39fb", "embedding": null, "extra_info": null, "node_info": { "end": 12360, "start": 11744 }, "relationships": { "1": "a07a2f85-7735-4648-b78f-a43b66bc38fb", "2": "85320698-eeef-4682-ad22-a1fc4d968807", "3": "729bf224-b65c-4285-94df-7492abf989d1" }, "text":"\ntext\n" }, "score": 0.8182768021866872 } ] } How can I get the usage and other information returned by OpenAI? Compared with the prediction, this really reflects my spending.

nasirus commented 1 year ago

Hi there,

Thank you for your question. To get the usage and other information returned by OpenAI, you can use the OpenAI API. The API provides a response object that contains the usage and other information. You can access the response object by using the index.query method.

Here is an example of how to use the index.query method to get the usage and other information returned by OpenAI:

# Initialize OpenAI object
openai = OpenAI(os.environ['OPENAI_API_KEY'])

# Get the response object
response = openai.query(prompt_text)

# Access the usage and other information
usage = response.usage
choices = response.choices

I hope this helps. Please let me know if you have any further questions.

Best, [Your Name]