mharrisb1 / openai-responses-python

๐Ÿงช๐Ÿค– Pytest plugin for OpenAI requests
https://mharrisb1.github.io/openai-responses-python/
MIT License
31 stars 3 forks source link

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

Closed ffreemt closed 2 months ago

ffreemt commented 2 months ago

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 commented 2 months ago

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 ๐Ÿ‘

ffreemt commented 2 months ago

Cool, thanks very much.