题意:如何为每个链指定LangSmith项目名称?
问题背景:
According to the LangSmith documentation you need to set the LANGCHAIN_PROJECT
environment variable to specify the project name in langsmith.
根据LangSmith的文档,您需要设置LANGCHAIN_PROJECT
环境变量来指定LangSmith中的项目名称。
However, if we want to execute multiple chains within a single service but under different project names, how do we specify the project name at runtime per-chain?
然而,如果我们想在一个服务内执行多个链,但每个链使用不同的项目名称,我们如何在运行时为每个链指定项目名称?
We think we have a solution, but it required looking into the LangChain code, so we are concerned this is not the idomatic way to do it and it might cause issues when their API changes.
我们认为我们有一个解决方案,但需要查看LangChain的代码,因此我们担心这不是一种标准做法,并且当他们的API发生变化时可能会引发问题。
This is what we think we should do. 这是我们应该做的
- Instantiate the chain. 实例化链。
- Instantiate a custom langchain tracer but with a specific project name defined at runtime.
实例化一个自定义的langchain追踪器,但在运行时定义一个特定的项目名称。
- Replace the chain's callbacks with the tracer.
用追踪器替换链的回调函数。
chain = LLMChain(llm=self._chat_model, prompt=prompt, verbose=True)
tracer = LangChainTracer(project_name="whatever")
chain.callbacks = CallbackManager(handlers=[tracer])
Thank you in advance for any help. 提前感谢您的任何帮助。
问题解决:
You can specify project name in the @traceable decorator as an argument.
您可以在@traceable
装饰器中作为参数来指定项目名称。
@traceable(
run_type="llm",
project_name="My Project"
)
def my_llm_call(
...
) -> str:
You can also put it as a part of langsmith_extra when calling the traced function. This takes precedence over the traceable project:
您还可以在调用被追踪的函数时,将其作为langsmith_extra
的一部分。这会优先于可追踪的项目设置:
my_llm_call(
...,
langsmith_extra={"project_name": "My Project 2"},
)