mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-20 01:54:14 +00:00
experimental[minor]: Add bind_tools and with_structured_output functions to OllamaFunctions (#20881)
Implemented bind_tools for OllamaFunctions. Made OllamaFunctions sub class of ChatOllama. Implemented with_structured_output for OllamaFunctions. integration unit test has been updated. notebook has been updated. --------- Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
@@ -2,9 +2,18 @@
|
||||
|
||||
import unittest
|
||||
|
||||
from langchain_community.chat_models.ollama import ChatOllama
|
||||
from langchain_core.messages import AIMessage
|
||||
from langchain_core.pydantic_v1 import BaseModel, Field
|
||||
|
||||
from langchain_experimental.llms.ollama_functions import OllamaFunctions
|
||||
from langchain_experimental.llms.ollama_functions import (
|
||||
OllamaFunctions,
|
||||
convert_to_ollama_tool,
|
||||
)
|
||||
|
||||
|
||||
class Joke(BaseModel):
|
||||
setup: str = Field(description="The setup of the joke")
|
||||
punchline: str = Field(description="The punchline to the joke")
|
||||
|
||||
|
||||
class TestOllamaFunctions(unittest.TestCase):
|
||||
@@ -13,12 +22,11 @@ class TestOllamaFunctions(unittest.TestCase):
|
||||
"""
|
||||
|
||||
def test_default_ollama_functions(self) -> None:
|
||||
base_model = OllamaFunctions(model="mistral")
|
||||
self.assertIsInstance(base_model.model, ChatOllama)
|
||||
base_model = OllamaFunctions(model="llama3", format="json")
|
||||
|
||||
# bind functions
|
||||
model = base_model.bind(
|
||||
functions=[
|
||||
model = base_model.bind_tools(
|
||||
tools=[
|
||||
{
|
||||
"name": "get_current_weather",
|
||||
"description": "Get the current weather in a given location",
|
||||
@@ -47,3 +55,29 @@ class TestOllamaFunctions(unittest.TestCase):
|
||||
function_call = res.additional_kwargs.get("function_call")
|
||||
assert function_call
|
||||
self.assertEqual(function_call.get("name"), "get_current_weather")
|
||||
|
||||
def test_ollama_structured_output(self) -> None:
|
||||
model = OllamaFunctions(model="phi3")
|
||||
structured_llm = model.with_structured_output(Joke, include_raw=False)
|
||||
|
||||
res = structured_llm.invoke("Tell me a joke about cats")
|
||||
assert isinstance(res, Joke)
|
||||
|
||||
def test_ollama_structured_output_with_json(self) -> None:
|
||||
model = OllamaFunctions(model="phi3")
|
||||
joke_schema = convert_to_ollama_tool(Joke)
|
||||
structured_llm = model.with_structured_output(joke_schema, include_raw=False)
|
||||
|
||||
res = structured_llm.invoke("Tell me a joke about cats")
|
||||
assert "setup" in res
|
||||
assert "punchline" in res
|
||||
|
||||
def test_ollama_structured_output_raw(self) -> None:
|
||||
model = OllamaFunctions(model="phi3")
|
||||
structured_llm = model.with_structured_output(Joke, include_raw=True)
|
||||
|
||||
res = structured_llm.invoke("Tell me a joke about cars")
|
||||
assert "raw" in res
|
||||
assert "parsed" in res
|
||||
assert isinstance(res["raw"], AIMessage)
|
||||
assert isinstance(res["parsed"], Joke)
|
||||
|
Reference in New Issue
Block a user