Coder Social home page Coder Social logo

Comments (3)

yihuazhai avatar yihuazhai commented on July 16, 2024

yes, I met the same issue as you. Even if not using with_structured_output, the with_config cannot propogate the paramters.

from langchain.

paul-tharun avatar paul-tharun commented on July 16, 2024

yes, I met the same issue as you. Even if not using with_structured_output, the with_config cannot propogate the paramters.

Do you have an alternative implementation you use?

from langchain.

paul-tharun avatar paul-tharun commented on July 16, 2024

A workaround if anyone encounters the same issue

from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI
from langchain_openai.chat_models.base import convert_to_openai_tool
from langchain_core.runnables import ConfigurableField
from typing import Dict, Any, Optional
from langchain_core.output_parsers.openai_tools import PydanticToolsParser

class Joke(BaseModel):
    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")


class ChatOpenAIWithStructedOutput(ChatOpenAI):
    output_structure: Optional[Dict[str, Any] ] = None
    
    @property
    def _default_params(self) -> Dict[str, Any]:
        """Get the default parameters for calling OpenAI API."""
        params = {
            "model": self.model_name,
            "stream": self.streaming,
            "n": self.n,
            "temperature": self.temperature,
            **self.model_kwargs,
        }
        if self.output_structure is not None:
            tools = [self.output_structure]
            tool_choice = {"type" : "function", "function" : {"name" : tools[0]["function"]["name"]}}
            params["tools"] = tools
            params["tool_choice"] = tool_choice
        if self.max_tokens is not None:
            params["max_tokens"] = self.max_tokens
        return params

model = ChatOpenAIWithStructedOutput(model="gpt-3.5-turbo-0125", temperature=2, api_key="API_KEY").configurable_fields(
                                temperature=ConfigurableField(id="temperature", name="Temperature", description="The temperature of the model"), 
                                output_structure=ConfigurableField(id="output_structure", name="output_structure", description="The output schema of the model"))


structured_llm = model.with_config(configurable={"output_structure" : convert_to_openai_tool(Joke)}) | PydanticToolsParser(tools=[Joke], first_tool_only=True)

structured_llm.with_config(configurable={"temperature" : 1}).invoke("Tell me a joke about cats")

from langchain.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.