https://github.com/prefecthq/langchain-prefect
Tools for using Langchain with Prefect
https://github.com/prefecthq/langchain-prefect
langchain large-language-models prefect python
Last synced: 9 months ago
JSON representation
Tools for using Langchain with Prefect
- Host: GitHub
- URL: https://github.com/prefecthq/langchain-prefect
- Owner: PrefectHQ
- License: apache-2.0
- Created: 2023-03-06T22:20:40.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-12T14:47:16.000Z (almost 3 years ago)
- Last Synced: 2025-01-20T23:02:44.189Z (over 1 year ago)
- Topics: langchain, large-language-models, prefect, python
- Language: Python
- Homepage: https://prefecthq.github.io/langchain-prefect/
- Size: 1.58 MB
- Stars: 103
- Watchers: 3
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# langchain-prefect
Read the [accompanying article](https://medium.com/the-prefect-blog/keeping-your-eyes-on-your-ai-tools-6428664537da) for background
## Orchestrate and observe langchain using Prefect
Large Language Models (LLMs) are interesting and useful - building apps that use them responsibly feels like a no-brainer. Tools like [Langchain](https://github.com/hwchase17/langchain) make it easier to build apps using LLMs. We need to know details about how our apps work, even when we want to use tools with convenient abstractions that may obfuscate those details.
Prefect is built to help data people build, run, and observe event-driven workflows wherever they want. It provides a framework for creating deployments on a whole slew of runtime environments (from Lambda to Kubernetes), and is cloud agnostic (best supports AWS, GCP, Azure). For this reason, it could be a great fit for observing apps that use LLMs.
## Features
- `RecordLLMCalls` is a `ContextDecorator` that can be used to track LLM calls made by Langchain LLMs as Prefect flows.
### Call an LLM and track the invocation with Prefect:
```python
from langchain.llms import OpenAI
from langchain_prefect.plugins import RecordLLMCalls
with RecordLLMCalls():
llm = OpenAI(temperature=0.9)
text = (
"What would be a good company name for a company that makes colorful socks?"
)
llm(text)
```
and a flow run will be created to track the invocation of the LLM:
### Run several LLM calls via langchain agent as Prefect subflows:
```python
from langchain.agents import initialize_agent, load_tools
from langchain.llms import OpenAI
from prefect import flow
llm = OpenAI(temperature=0)
tools = load_tools(["llm-math"], llm=llm)
agent = initialize_agent(tools, llm)
@flow
def my_flow():
agent.run(
"How old is the current Dalai Lama? "
"What is his age divided by 2 (rounded to the nearest integer)?"
)
with RecordLLMCalls(tags={"agent"}):
my_flow()
```
Find more examples [here](examples/).
## How do I get a Prefect UI?
- The easiest way is to use the [Prefect Cloud](https://www.prefect.io/cloud/) UI for free. You can find details on getting setup [here](https://docs.prefect.io/ui/cloud-quickstart/).
- If you don't want to sign up for cloud, you can use the dashboard locally by running `prefect server start` in your terminal - more details [here](https://docs.prefect.io/ui/overview/#using-the-prefect-ui).
## Resources
### Installation
```bash
pip install langchain-prefect
```
Requires an installation of Python 3.10+.
### Feedback
If you encounter any bugs while using `langchain-prefect`, feel free to open an issue in the [langchain-prefect](https://github.com/PrefectHQ/langchain-prefect) repository.
Feel free to star or watch [`langchain-prefect`](https://github.com/PrefectHQ/langchain-prefect) for updates too!
### Contributing
If you'd like to help contribute to fix an issue or add a feature to `langchain-prefect`, please [propose changes through a pull request from a fork of the repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork).
Here are the steps:
1. [Fork the repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo#forking-a-repository)
2. [Clone the forked repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo#cloning-your-forked-repository)
3. Install the repository and its dependencies:
```
pip install -e ".[dev]"
```
4. Make desired changes
5. Add tests
6. Insert an entry to [CHANGELOG.md](https://github.com/PrefectHQ/langchain-prefect/blob/main/CHANGELOG.md)
7. Install `pre-commit` to perform quality checks prior to commit:
```
pre-commit install
```
8. `git commit`, `git push`, and create a pull request