Skip to content

Commit 7a8c2dd

Browse files
committed
changed to hopefully finally have acceptable number of local vars
1 parent 8a4e6d5 commit 7a8c2dd

File tree

1 file changed

+27
-39
lines changed
  • ai_agent_instrumentation/opentelemetry-instrumentation-langchain-v2/tests

1 file changed

+27
-39
lines changed

ai_agent_instrumentation/opentelemetry-instrumentation-langchain-v2/tests/test_chains.py

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ def create_bedrock_llm(region="us-west-2"):
2626

2727

2828
def create_chains(llm):
29-
"""Create and return the synopsis chain, review chain, and overall chain."""
30-
29+
"""Create and return the sequential chain."""
3130
synopsis_prompt = PromptTemplate(
3231
input_variables=["title", "era"],
3332
template="""You are a playwright. Given the title of play and the era it is set in, it is your job to write a synopsis for that title.
@@ -46,7 +45,7 @@ def create_chains(llm):
4645
Review from a New York Times play critic of the above play:""", # noqa: E501
4746
)
4847

49-
overall_chain = SequentialChain(
48+
return SequentialChain(
5049
chains=[
5150
LLMChain(llm=llm, prompt=synopsis_prompt, output_key="synopsis", name="synopsis"),
5251
LLMChain(llm=llm, prompt=review_prompt, output_key="review"),
@@ -56,56 +55,45 @@ def create_chains(llm):
5655
verbose=True,
5756
)
5857

59-
return overall_chain
60-
61-
62-
def validate_span(span, expected_kind, expected_attrs):
63-
"""Validate a span against expected values."""
64-
assert span.kind == expected_kind
65-
for attr in expected_attrs:
66-
assert attr in span.attributes
67-
return ast.literal_eval(span.attributes["gen_ai.prompt"]), ast.literal_eval(span.attributes["gen_ai.completion"])
68-
6958

7059
@pytest.mark.vcr(filter_headers=["Authorization", "X-Amz-Date", "X-Amz-Security-Token"], record_mode="once")
7160
def test_sequential_chain(instrument_langchain, span_exporter):
7261
span_exporter.clear()
7362

74-
llm = create_bedrock_llm()
75-
chain = create_chains(llm)
7663
input_data = {"title": "Tragedy at sunset on the beach", "era": "Victorian England"}
77-
chain.invoke(input_data)
64+
create_chains(create_bedrock_llm()).invoke(input_data)
7865

7966
spans = span_exporter.get_finished_spans()
80-
langchain_spans = [span for span in spans if span.name.startswith("chain ")]
81-
82-
assert [
83-
"chain synopsis",
84-
"chain LLMChain",
85-
"chain SequentialChain",
86-
] == [span.name for span in langchain_spans]
87-
8867
synopsis_span = next(span for span in spans if span.name == "chain synopsis")
8968
review_span = next(span for span in spans if span.name == "chain LLMChain")
9069
overall_span = next(span for span in spans if span.name == "chain SequentialChain")
9170

92-
synopsis_prompt, synopsis_completion = validate_span(
93-
synopsis_span, SpanKind.INTERNAL, ["gen_ai.prompt", "gen_ai.completion"]
71+
assert ["chain synopsis", "chain LLMChain", "chain SequentialChain"] == [
72+
span.name for span in spans if span.name.startswith("chain ")
73+
]
74+
75+
for span in [synopsis_span, review_span, overall_span]:
76+
assert span.kind == SpanKind.INTERNAL
77+
assert "gen_ai.prompt" in span.attributes
78+
assert "gen_ai.completion" in span.attributes
79+
80+
synopsis_data = (
81+
ast.literal_eval(synopsis_span.attributes["gen_ai.prompt"]),
82+
ast.literal_eval(synopsis_span.attributes["gen_ai.completion"]),
9483
)
95-
assert synopsis_prompt == input_data
96-
assert "synopsis" in synopsis_completion
84+
assert synopsis_data[0] == input_data
85+
assert "synopsis" in synopsis_data[1]
9786

98-
review_prompt, review_completion = validate_span(
99-
review_span, SpanKind.INTERNAL, ["gen_ai.prompt", "gen_ai.completion"]
87+
review_data = (
88+
ast.literal_eval(review_span.attributes["gen_ai.prompt"]),
89+
ast.literal_eval(review_span.attributes["gen_ai.completion"]),
10090
)
101-
assert "title" in review_prompt
102-
assert "era" in review_prompt
103-
assert "synopsis" in review_prompt
104-
assert "review" in review_completion
91+
assert all(key in review_data[0] for key in ["title", "era", "synopsis"])
92+
assert "review" in review_data[1]
10593

106-
overall_prompt, overall_completion = validate_span(
107-
overall_span, SpanKind.INTERNAL, ["gen_ai.prompt", "gen_ai.completion"]
94+
overall_data = (
95+
ast.literal_eval(overall_span.attributes["gen_ai.prompt"]),
96+
ast.literal_eval(overall_span.attributes["gen_ai.completion"]),
10897
)
109-
assert overall_prompt == input_data
110-
assert "synopsis" in overall_completion
111-
assert "review" in overall_completion
98+
assert overall_data[0] == input_data
99+
assert all(key in overall_data[1] for key in ["synopsis", "review"])

0 commit comments

Comments
 (0)