How to access config variables in a tool ? #22529
Replies: 2 comments 13 replies
-
Hey there, @mathlover777! I'm here to assist you with any questions or issues you have. Let's tackle this together! To access configuration variables, such as the from langchain_core.beta.runnables.context import Context
from pymongo import MongoClient
import os
class getUserInfoInput(BaseModel):
phone: str = Field(description="Users phone number")
@tool("save_user", args_schema=getUserInfoInput)
def save_user(phone: str) -> dict:
"""Save the user detail"""
# Retrieve the current sessionId using Context
current_session_id = Context.getter("sessionId").invoke(None)
# Connect to MongoDB
client = MongoClient(os.environ["MONGO_CONNECTION_STRING"])
db = client[os.environ["MONGO_DB"]]
collection = db[os.environ["MONGO_COLLECTION"]]
# Save the user detail along with the sessionId
collection.insert_one({
"sessionId": current_session_id,
"phone": phone
})
return {"status": "success", "sessionId": current_session_id}
tools = [get_food_price, save_user] In this example, |
Beta Was this translation helpful? Give feedback.
-
@mathlover777 tools don't currently expose the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I am using Langserve to create a langchain agent with tool calling. Till now all is working as expected and I am able to host it in AWS ECS with CDK also and learnt a lot in the process. But one thing I am stuck, in my chat, users will give their phone number and I will store the user as a lead in a crm I built using mongoDB. However, in the crm I want to show the chat history also.
For the chat history, I am using the mongodb integration and its working as expected when I pass the sessionId in the api calls.
Now I want to create a tool say
save_user
which will take the phone number as input. I tested dummy tool calls and its working. but in the save_user function, I want to save the phone number and the associated sessionId. I did not find any documentation or tutorial on how to achieve this.This is the tool
System Info
Beta Was this translation helpful? Give feedback.
All reactions