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

fix(openai): langchain streaming bug #225

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def _set_response_attributes(span, llm_request_type, response):
def _build_from_streaming_response(span, llm_request_type, response):
complete_response = {"choices": [], "model": ""}
for item in response:
item_to_yield = item
if is_openai_v1():
item = item.__dict__

Expand Down Expand Up @@ -234,7 +235,7 @@ def _build_from_streaming_response(span, llm_request_type, response):
else:
complete_choice["text"] += choice.get("text")

yield item
yield item_to_yield

_set_response_attributes(
span,
Expand Down
26 changes: 25 additions & 1 deletion packages/traceloop-sdk/tests/test_langchain_instrumentation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.schema import StrOutputParser
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain import LLMChain
from langchain.chains import SequentialChain

Expand Down Expand Up @@ -38,7 +41,6 @@ def test_langchain(exporter):
)

spans = exporter.get_finished_spans()
print([span.name for span in spans])

assert set(
[
Expand All @@ -48,3 +50,25 @@ def test_langchain(exporter):
"langchain.workflow",
]
).issubset([span.name for span in spans])


def test_langchain_streaming(exporter):
chat = ChatOpenAI(
model="gpt-4",
temperature=0,
streaming=True,
)

prompt = PromptTemplate.from_template(
"write 10 lines of random text about ${product}"
)
runnable = prompt | chat | StrOutputParser()
runnable.invoke({"product": "colorful socks"})

spans = exporter.get_finished_spans()

assert set(
[
"openai.chat",
]
).issubset([span.name for span in spans])
Loading