traceloop / openllmetry

Open-source observability for your LLM application, based on OpenTelemetry
https://www.traceloop.com/openllmetry
Apache License 2.0
1.55k stars 122 forks source link

🚀 Feature: re-write Langchain instrumentation to use Langchain Callbacks #541

Open nirga opened 4 months ago

nirga commented 4 months ago

Which component is this feature for?

Langchain Instrumentation

🔖 Feature description

Right now, we monkey-patch classes and methods in LlamaIndex which requires endless work and constant maintenance. Langchain has a system for callbacks that can potentially be used to create/end spans without being too coupled with with the framework's inner structure.

🎤 Why is this feature needed ?

Support Langchain entirely and be future-proof to internal API changes

✌️ How do you aim to achieve this?

Look into Langchain callbacks and how other frameworks are using it.

🔄️ Additional Information

No response

👀 Have you spent some time to check if this feature request has been raised before?

Are you willing to submit PR?

None

maciejwie commented 4 months ago

Chainlit uses langchain's callbacks for their instrumentation, and it seems to work well enough. They inherit from langchain's BaseTracer class as well and do their observability through callbacks, as well as some front-end functions (ex: updating the user-facing message on each token). Langchain does allow for multiple independent callbacks to be specified, so doing it this way doesn't exclude any user-created callbacks.

midhun1998 commented 4 months ago

Hi @nirga , I have a rough idea on the implementation now and I am willing to pick up this issue. After reading your comments in https://github.com/traceloop/openllmetry-js/issues/133#issuecomment-1999619802 here is my understanding:

  1. Create a call handler similar to StdOutCallbackHandler in the same directory as opentelemetry-instrumentation-langchain
  2. Modify the task_wrapper, atask_wrapper, workflow_wrapper, and aworkflow_wrapper to inject the callback handler to the instance and use the callback to set/unset span. A small doubt here is that do we still need a task_wrapper.py and workflow_wrapper.py? Can I add the injecting logic to __init__.py itself. Which one's the right approach?
  3. The callback handler should be injected for Langchain classes instances which support callbacks.

Please correct me if my understanding is incorrect. Thanks, Midhun

nirga commented 4 months ago

Right @midhun1998! And indeed we probably don't need them all

midhun1998 commented 4 months ago

Thanks for the confirmation, Nir! I will keep the issue updated with the progress. 🙂

midhun1998 commented 3 months ago

Hi @nirga ,

I tried the approach suggested and met with some roadblocks. Need your input. Below are the details:

Observation and Notes:

  1. The callback handler only applies to very few classes such as Chain, Agent, Tool, and lacks the support for other classes which were used earlier such as Template, BasePromptTemplate BaseOutputParser, RunnableSequence, etc. How are we looking to support these? Do we maintain the monkey patching for methods for the others?
nirga commented 3 months ago

Linking here our slack conversation so I'll remember that we've discussed and answered these already 😅