Token count in sql agent. #21657
Replies: 3 comments 2 replies
-
To address the issue of token counts and costs not updating as expected in your SQL agent, consider the following adjustments:
Here's a snippet to guide the adjustments, particularly around callback management: from langchain_core.callbacks import CallbackManager
# Create a callback manager and register your callback handler
callback_manager = CallbackManager()
callback_handler = TokenCountCallbackHandler()
callback_manager.register(callback_handler)
# Pass the callback manager to the ChatOpenAI constructor
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0, streaming=True, callback_manager=callback_manager) This approach ensures your
|
Beta Was this translation helpful? Give feedback.
-
I got the error about handler first and then I changed the code to llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0, streaming=True, callback_manager=[callback_manager]) |
Beta Was this translation helpful? Give feedback.
-
The error I was encountering has resolved. But the token count is still 0
|
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I am trying to count the number of tokens for my sql agent I have made. For this, I have made a custom callback handler which is present in code. I am unable to count the tokens. The number of tokens and cost are being 0 in the response. Can you help me figure out the issue?
Below is my output:
Invoking:
sql_db_list_tables
with{}
/home/usama/Forloops/langchainDB/.venv/lib/python3.10/site-packages/langchain_community/utilities/sql_database.py:313: SAWarning: Cannot correctly sort tables; there are unresolvable cycles between tables "Logistics_brands, UserManagement_user", which is usually caused by mutually dependent foreign key constraints. Foreign key constraints involving these tables will not be considered; this warning may raise an error in a future release.
metadata_table_names = [tbl.name for tbl in self._metadata.sorted_tables]
/home/usama/Forloops/langchainDB/.venv/lib/python3.10/site-packages/langchain_community/utilities/sql_database.py:325: SAWarning: Cannot correctly sort tables; there are unresolvable cycles between tables "Logistics_brands, UserManagement_user", which is usually caused by mutually dependent foreign key constraints. Foreign key constraints involving these tables will not be considered; this warning may raise an error in a future release.
for tbl in self._metadata.sorted_tables
CREATE TABLE
CustomerSupport_activity
(id BIGINT NOT NULL AUTO_INCREMENT,
created_at DATETIME(6),
updated_at DATETIME(6),
status VARCHAR(100) NOT NULL,
query_id BIGINT NOT NULL,
user_id BIGINT,
PRIMARY KEY (id),
CONSTRAINT
CustomerSupport_acti_query_id_0f31d07e_fk_CustomerS
FOREIGN KEY(query_id) REFERENCESCustomerSupport_supportqueries
(id),CONSTRAINT
CustomerSupport_acti_user_id_8955ebe3_fk_UserManag
FOREIGN KEY(user_id) REFERENCESUserManagement_user
(id))ENGINE=InnoDB COLLATE utf8mb4_0900_ai_ci DEFAULT CHARSET=utf8mb4
/*
3 rows from CustomerSupport_activity table:
id created_at updated_at status query_id user_id
1 2024-03-28 05:22:23.924028 2024-03-28 05:22:23.924060 Status is Pending 4132 2374
2 2024-03-28 05:22:23.924087 2024-03-28 05:22:23.924094 Query is -> request4132 2374
3 2024-03-28 05:22:23.924108 2024-03-28 05:22:23.924114 Query type -> re-attempt 4132 2374
*/The
CustomerSupport_activity
table contains the following columns:CustomerSupport_supportqueries
tableUserManagement_user
tableHere are 3 sample rows from the
CustomerSupport_activity
table:If you have any specific questions or need further details, feel free to ask!
System Info
langchain==0.1.20
langchain_core==0.1.52
langchain_community==0.0.38
langchain_openai==0.1.6
Beta Was this translation helpful? Give feedback.
All reactions