mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
Add easy print method to openai callback (#2848)
Found myself constantly copying the snippet outputting all the callback tracking details. so adding a simple way to output the full context
This commit is contained in:
parent
be4fb24b32
commit
70ffe470aa
@ -43,22 +43,18 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Total Tokens: 39\n",
|
"Tokens Used: 42\n",
|
||||||
"Prompt Tokens: 4\n",
|
"\tPrompt Tokens: 4\n",
|
||||||
"Completion Tokens: 35\n",
|
"\tCompletion Tokens: 38\n",
|
||||||
"Successful Requests: 1\n",
|
"Successful Requests: 1\n",
|
||||||
"Total Cost (USD): $0.0007800000000000001\n"
|
"Total Cost (USD): $0.00084\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"with get_openai_callback() as cb:\n",
|
"with get_openai_callback() as cb:\n",
|
||||||
" result = llm(\"Tell me a joke\")\n",
|
" result = llm(\"Tell me a joke\")\n",
|
||||||
" print(f\"Total Tokens: {cb.total_tokens}\")\n",
|
" print(cb)"
|
||||||
" print(f\"Prompt Tokens: {cb.prompt_tokens}\")\n",
|
|
||||||
" print(f\"Completion Tokens: {cb.completion_tokens}\")\n",
|
|
||||||
" print(f\"Successful Requests: {cb.successful_requests}\")\n",
|
|
||||||
" print(f\"Total Cost (USD): ${cb.total_cost}\")"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -53,6 +53,15 @@ class OpenAICallbackHandler(BaseCallbackHandler):
|
|||||||
successful_requests: int = 0
|
successful_requests: int = 0
|
||||||
total_cost: float = 0.0
|
total_cost: float = 0.0
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return (
|
||||||
|
f"Tokens Used: {self.total_tokens}\n"
|
||||||
|
f"\tPrompt Tokens: {self.prompt_tokens}\n"
|
||||||
|
f"\tCompletion Tokens: {self.completion_tokens}\n"
|
||||||
|
f"Successful Requests: {self.successful_requests}\n"
|
||||||
|
f"Total Cost (USD): ${self.total_cost}"
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def always_verbose(self) -> bool:
|
def always_verbose(self) -> bool:
|
||||||
"""Whether to call verbose callbacks even if verbose is False."""
|
"""Whether to call verbose callbacks even if verbose is False."""
|
||||||
|
Loading…
Reference in New Issue
Block a user