Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Would openai-responses-python work with langchain_openai? #58

Closed
ffreemt opened this issue Aug 10, 2024 · 2 comments
Closed

Would openai-responses-python work with langchain_openai? #58

ffreemt opened this issue Aug 10, 2024 · 2 comments
Assignees
Labels
documentation Improvements or additions to documentation e0 🌵 Low effort i0 🌵 Low impact question Further information is requested

Comments

@ffreemt
Copy link

ffreemt commented Aug 10, 2024

Hello.

Maybe it's a silly question but I had a hard time trying pytest with langchain_openai. Can your lib work with langchain_openai with some tweaking? Thanks.

I ran a quick test.

import langchain_openai as openai

import openai_responses


@openai_responses.mock()
def test_create_assistant():
    client = openai.Client(api_key="sk-fake123")

    assistant = client.beta.assistants.create(
        instructions="You are a personal math tutor.",
        name="Math Tutor",
        tools=[{"type": "code_interpreter"}],
        model="gpt-4-turbo",
    )

    assert assistant.name == "Math Tutor"

I tried this (replacing openai with langchain_openai), but got

AttributeError: module 'langchain_openai' has no attribute 'Client'

when running test_create_assistant(), probably for an obvious reason.

@mharrisb1
Copy link
Owner

mharrisb1 commented Aug 10, 2024

Not a silly question.

langchain-openai uses openai under the hood so everything will work normally. The same goes for any API wrapper other than just Langchain as long as one of the two are true:

  1. They use openai library directly
  2. They use HTTPX or HTTP Core to make the requests to the OpenAI API (so a package using requests would not work)

Here is a working example using this library to test langchain-openai using ChatOpenAI that just slightly alters the normal chat completion example:

from langchain_openai import ChatOpenAI
from pydantic.v1 import SecretStr

import openai_responses
from openai_responses import OpenAIMock


@openai_responses.mock()
def test_langchain_chat_openai_invoke(openai_mock: OpenAIMock):
    openai_mock.chat.completions.create.response = {
        "choices": [
            {
                "index": 0,
                "finish_reason": "stop",
                "message": {
                    "content": "J'adore la programmation.",
                    "role": "assistant",
                },
            }
        ]
    }

    llm = ChatOpenAI(
        name="My Custom Chatbot",
        model="gpt-4o",
        temperature=0,
        max_tokens=None,
        timeout=None,
        max_retries=2,
        api_key=SecretStr("sk-fake123"),
    )

    messages = [
        (
            "system",
            "You are a helpful assistant that translates English to French. Translate the user sentence.",
        ),
        ("human", "I love programming."),
    ]
    ai_msg = llm.invoke(messages)
    assert ai_msg.content == "J'adore la programmation."  # type: ignore

I'll add this to the examples/ for others to see 👍

@github-project-automation github-project-automation bot moved this from Todo to Done in openai-responses-python Aug 10, 2024
@mharrisb1 mharrisb1 self-assigned this Aug 10, 2024
@mharrisb1 mharrisb1 added question Further information is requested e0 🌵 Low effort i0 🌵 Low impact documentation Improvements or additions to documentation labels Aug 10, 2024
@ffreemt
Copy link
Author

ffreemt commented Aug 11, 2024

Cool, thanks very much.

mharrisb1 added a commit that referenced this issue Aug 12, 2024
* chore(examples): add langchain-openai example from #58

* chore(types): add `refusal` to choice

* chore(deps): add langchain-openai as dev dep for testing

* chore(docs): remove not about support for older SDK versions

* chore(routes): move beta routes to separate dir

* chore(types): add structured output type additions

* feat(routes): add beta parsed chat route

* feat(examples): add parsed chat completion examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation e0 🌵 Low effort i0 🌵 Low impact question Further information is requested
Projects
Development

No branches or pull requests

2 participants