Skip to content

Commit 53081bb

Browse files
author
Andrei Bratu
committed
more flow decorators
1 parent 911db83 commit 53081bb

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

tests/integration/test_decorators.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from openai import OpenAI
55
from humanloop.client import Humanloop
6+
from humanloop.types.chat_message import ChatMessage
67

78

89
def test_prompt_decorator(
@@ -44,3 +45,110 @@ def my_prompt(question: str) -> str:
4445
assert logs_response.items is not None and len(logs_response.items) == 1
4546
finally:
4647
humanloop_test_client.prompts.delete(id=prompt_response.id)
48+
49+
50+
def test_call_prompt_in_flow_decorator(
51+
humanloop_test_client: Humanloop,
52+
sdk_test_dir: str,
53+
openai_key: str,
54+
):
55+
try:
56+
57+
@humanloop_test_client.flow(path=f"{sdk_test_dir}/test_flow")
58+
def my_flow(question: str) -> str:
59+
response = humanloop_test_client.prompts.call(
60+
path=f"{sdk_test_dir}/test_prompt",
61+
prompt={
62+
"provider": "openai",
63+
"model": "gpt-4o-mini",
64+
"temperature": 0,
65+
},
66+
messages=[{"role": "user", "content": question}],
67+
provider_api_keys={"openai": openai_key},
68+
)
69+
70+
assert response.logs[0].output is not None
71+
return response.logs[0].output
72+
73+
assert "paris" in my_flow("What is the capital of the France?").lower()
74+
time.sleep(5)
75+
prompt_response = humanloop_test_client.files.retrieve_by_path(path=f"{sdk_test_dir}/test_prompt")
76+
assert prompt_response is not None
77+
prompt_logs_response = humanloop_test_client.logs.list(file_id=prompt_response.id, page=1, size=50)
78+
assert prompt_logs_response.items is not None and len(prompt_logs_response.items) == 1
79+
prompt_log = prompt_logs_response.items[0]
80+
81+
flow_response = humanloop_test_client.files.retrieve_by_path(path=f"{sdk_test_dir}/test_flow")
82+
assert flow_response is not None
83+
flow_logs_response = humanloop_test_client.logs.list(file_id=flow_response.id, page=1, size=50)
84+
assert flow_logs_response.items is not None and len(flow_logs_response.items) == 1
85+
flow_log = flow_logs_response.items[0]
86+
assert prompt_log.trace_parent_id == flow_log.id
87+
finally:
88+
flow_response = humanloop_test_client.files.retrieve_by_path(path=f"{sdk_test_dir}/test_flow")
89+
if flow_response is not None:
90+
humanloop_test_client.flows.delete(id=flow_response.id)
91+
prompt_response = humanloop_test_client.files.retrieve_by_path(path=f"{sdk_test_dir}/test_prompt")
92+
if prompt_response is not None:
93+
humanloop_test_client.prompts.delete(id=prompt_response.id)
94+
95+
96+
def test_flow_decorator_logs_exceptions(
97+
humanloop_test_client: Humanloop,
98+
sdk_test_dir: str,
99+
):
100+
try:
101+
102+
@humanloop_test_client.flow(path=f"{sdk_test_dir}/test_flow_log_error")
103+
def my_flow(question: str) -> str:
104+
raise ValueError("This is a test exception")
105+
106+
my_flow("test")
107+
108+
time.sleep(5)
109+
110+
flow_response = humanloop_test_client.files.retrieve_by_path(path=f"{sdk_test_dir}/test_flow_log_error")
111+
assert flow_response is not None
112+
flow_logs_response = humanloop_test_client.logs.list(file_id=flow_response.id, page=1, size=50)
113+
assert flow_logs_response.items is not None and len(flow_logs_response.items) == 1
114+
flow_log = flow_logs_response.items[0]
115+
assert flow_log.error is not None
116+
assert flow_log.output is None
117+
118+
finally:
119+
flow_response = humanloop_test_client.files.retrieve_by_path(path=f"{sdk_test_dir}/test_flow_log_error")
120+
if flow_response is not None:
121+
humanloop_test_client.flows.delete(id=flow_response.id)
122+
123+
124+
def test_flow_decorator_populates_output_message(
125+
humanloop_test_client: Humanloop,
126+
sdk_test_dir: str,
127+
):
128+
try:
129+
130+
@humanloop_test_client.flow(path=f"{sdk_test_dir}/test_flow_log_output_message")
131+
def my_flow(question: str) -> dict[str, Any]:
132+
return {"role": "user", "content": question}
133+
134+
assert "france" in my_flow("What is the capital of the France?")["content"].lower()
135+
136+
time.sleep(5)
137+
138+
flow_response = humanloop_test_client.files.retrieve_by_path(
139+
path=f"{sdk_test_dir}/test_flow_log_output_message"
140+
)
141+
assert flow_response is not None
142+
flow_logs_response = humanloop_test_client.logs.list(file_id=flow_response.id, page=1, size=50)
143+
assert flow_logs_response.items is not None and len(flow_logs_response.items) == 1
144+
flow_log = flow_logs_response.items[0]
145+
assert flow_log.output_message is not None
146+
assert flow_log.output is None
147+
assert flow_log.error is None
148+
149+
finally:
150+
flow_response = humanloop_test_client.files.retrieve_by_path(
151+
path=f"{sdk_test_dir}/test_flow_log_output_message"
152+
)
153+
if flow_response is not None:
154+
humanloop_test_client.flows.delete(id=flow_response.id)

0 commit comments

Comments
 (0)