Skip to content

Commit

Permalink
Add langchain integration
Browse files Browse the repository at this point in the history
  • Loading branch information
synacktraa committed Sep 22, 2024
1 parent 56484cc commit da42cbe
Show file tree
Hide file tree
Showing 13 changed files with 5,049 additions and 216 deletions.
4 changes: 2 additions & 2 deletions .github/actions/setup-poetry-env/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ runs:

- name: Install dependencies on Windows
if: runner.os == 'Windows'
run: poetry install -E pydantic --no-interaction
run: poetry install -E langchain --no-interaction
shell: pwsh

- name: Install dependencies on Linux/macOS
if: runner.os != 'Windows'
run: poetry install -E pydantic --no-interaction
run: poetry install -E langchain --no-interaction
shell: bash
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: install
install: ## Install the poetry environment and install the pre-commit hooks
@echo "🚀 Creating virtual environment using poetry"
@poetry install -E pydantic
@poetry install -E langchain
@poetry run pre-commit install
@poetry shell

Expand Down
60 changes: 59 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ pip install tool-parse
```

- with `pydantic` support

```sh
pip install "tool-parse[pydantic]"
```

- with `langchain` based integration
```sh
pip install "tool-parse[langchain]
```
## 🌟 Key Features
1. **Versatile Tool Management:**
Expand Down Expand Up @@ -73,7 +79,8 @@ pip install tool-parse
## Cookbooks
- [GorillaLLM Integration](https://colab.research.google.com/drive/1C2WCgIZ7LnkpLt3KARL9ROh4iLwaACa6?usp=sharing)
- [GorillaLLM Integration](./cookbooks/gorillaLLM-integration.ipynb)
- [Langgraph+Ollama Example](./cookbooks//langgraph-ollama-example.ipynb)
## Usage 🤗
Expand Down Expand Up @@ -269,6 +276,57 @@ def calculate_discount(
combined_registry = tr + new_registry
```
## Third Party Integrations
### Langchain
Define the tools
```python
from tool_parse.integrations.langchain import ExtendedStructuredTool
async def search_web(query: str, safe_search: bool = True):
"""
Search the web.
:param query: Query to search for.
:param safe_search: If True, enable safe search.
"""
return "not found"
class UserInfo(NamedTuple):
"""User information"""
name: str
age: int
role: Literal['admin', 'tester'] = 'tester'
tools = [
ExtendedStructuredTool(func=search_web),
ExtendedStructuredTool(func=UserInfo, name="user_info", schema_spec='claude'),
]
# OR
tools = ExtendedStructuredTool.from_objects(search_web, UserInfo, schema_spec='base')
```
Patch the chat model
```python
from langchain_ollama.chat_models import ChatOllama
from tool_parse.integrations.langchain import patch_chat_model
model = patch_chat_model(ChatOllama(model="llama3-groq-tool-use")) # Patch the instance
# OR
model = patch_chat_model(ChatOllama)(model="llama3-groq-tool-use") # Patch the class and then instantiate it
```
Bind the tools
```python
model.bind_tools(tools=tools)
```
> For langgraph agent usage, refer [Langgraph+Ollama Example](./cookbooks//langgraph-ollama-example.ipynb) cookbook
## 🤝 Contributing
Contributions, issues, and feature requests are welcome! Feel free to check the [issues page](https://github.com/synacktraa/tool-parse/issues).
Expand Down
Loading

0 comments on commit da42cbe

Please sign in to comment.