22from typing import Any
33
44from openai import OpenAI
5- from humanloop . client import Humanloop
5+ from tests . custom . integration . conftest import GetHumanloopClientFn
66
77
88def test_prompt_decorator (
9- humanloop_test_client : Humanloop ,
9+ get_humanloop_client : GetHumanloopClientFn ,
1010 sdk_test_dir : str ,
1111 test_prompt_config : dict [str , Any ],
1212 openai_key : str ,
1313):
1414 try :
15+ humanloop_client = get_humanloop_client ()
1516 prompt_path = f"{ sdk_test_dir } /test_prompt"
16- prompt_response = humanloop_test_client .prompts .upsert (
17+ prompt_response = humanloop_client .prompts .upsert (
1718 path = prompt_path ,
1819 ** test_prompt_config ,
1920 )
2021
21- prompt_versions_response = humanloop_test_client .prompts .list_versions (id = prompt_response .id )
22+ prompt_versions_response = humanloop_client .prompts .list_versions (id = prompt_response .id )
2223 assert len (prompt_versions_response .records ) == 1
2324
24- @humanloop_test_client .prompt (path = prompt_path )
25+ @humanloop_client .prompt (path = prompt_path )
2526 def my_prompt (question : str ) -> str :
2627 openai_client = OpenAI (api_key = openai_key )
2728
@@ -36,26 +37,27 @@ def my_prompt(question: str) -> str:
3637 assert "paris" in my_prompt ("What is the capital of the France?" ).lower ()
3738
3839 time .sleep (5 )
39- prompt_versions_response = humanloop_test_client .prompts .list_versions (id = prompt_response .id )
40+ prompt_versions_response = humanloop_client .prompts .list_versions (id = prompt_response .id )
4041 assert len (prompt_versions_response .records ) == 2
4142
42- logs_response = humanloop_test_client .logs .list (file_id = prompt_response .id , page = 1 , size = 50 )
43+ logs_response = humanloop_client .logs .list (file_id = prompt_response .id , page = 1 , size = 50 )
4344
4445 assert logs_response .items is not None and len (logs_response .items ) == 1
4546 finally :
46- humanloop_test_client .prompts .delete (id = prompt_response .id )
47+ humanloop_client .prompts .delete (id = prompt_response .id )
4748
4849
4950def test_call_prompt_in_flow_decorator (
50- humanloop_test_client : Humanloop ,
51+ get_humanloop_client : GetHumanloopClientFn ,
5152 sdk_test_dir : str ,
5253 openai_key : str ,
5354):
5455 try :
56+ humanloop_client = get_humanloop_client ()
5557
56- @humanloop_test_client .flow (path = f"{ sdk_test_dir } /test_flow" )
58+ @humanloop_client .flow (path = f"{ sdk_test_dir } /test_flow" )
5759 def my_flow (question : str ) -> str :
58- response = humanloop_test_client .prompts .call (
60+ response = humanloop_client .prompts .call (
5961 path = f"{ sdk_test_dir } /test_prompt" ,
6062 prompt = {
6163 "provider" : "openai" ,
@@ -71,83 +73,81 @@ def my_flow(question: str) -> str:
7173
7274 assert "paris" in my_flow ("What is the capital of the France?" ).lower ()
7375 time .sleep (5 )
74- prompt_response = humanloop_test_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_prompt" )
76+ prompt_response = humanloop_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_prompt" )
7577 assert prompt_response is not None
76- prompt_logs_response = humanloop_test_client .logs .list (file_id = prompt_response .id , page = 1 , size = 50 )
78+ prompt_logs_response = humanloop_client .logs .list (file_id = prompt_response .id , page = 1 , size = 50 )
7779 assert prompt_logs_response .items is not None and len (prompt_logs_response .items ) == 1
7880 prompt_log = prompt_logs_response .items [0 ]
7981
80- flow_response = humanloop_test_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_flow" )
82+ flow_response = humanloop_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_flow" )
8183 assert flow_response is not None
82- flow_logs_response = humanloop_test_client .logs .list (file_id = flow_response .id , page = 1 , size = 50 )
84+ flow_logs_response = humanloop_client .logs .list (file_id = flow_response .id , page = 1 , size = 50 )
8385 assert flow_logs_response .items is not None and len (flow_logs_response .items ) == 1
8486 flow_log = flow_logs_response .items [0 ]
8587 assert prompt_log .trace_parent_id == flow_log .id
8688 finally :
87- flow_response = humanloop_test_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_flow" )
89+ flow_response = humanloop_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_flow" )
8890 if flow_response is not None :
89- humanloop_test_client .flows .delete (id = flow_response .id )
90- prompt_response = humanloop_test_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_prompt" )
91+ humanloop_client .flows .delete (id = flow_response .id )
92+ prompt_response = humanloop_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_prompt" )
9193 if prompt_response is not None :
92- humanloop_test_client .prompts .delete (id = prompt_response .id )
94+ humanloop_client .prompts .delete (id = prompt_response .id )
9395
9496
9597def test_flow_decorator_logs_exceptions (
96- humanloop_test_client : Humanloop ,
98+ get_humanloop_client : GetHumanloopClientFn ,
9799 sdk_test_dir : str ,
98100):
99101 try :
102+ humanloop_client = get_humanloop_client ()
100103
101- @humanloop_test_client .flow (path = f"{ sdk_test_dir } /test_flow_log_error" )
104+ @humanloop_client .flow (path = f"{ sdk_test_dir } /test_flow_log_error" )
102105 def my_flow (question : str ) -> str :
103106 raise ValueError ("This is a test exception" )
104107
105108 my_flow ("test" )
106109
107110 time .sleep (5 )
108111
109- flow_response = humanloop_test_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_flow_log_error" )
112+ flow_response = humanloop_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_flow_log_error" )
110113 assert flow_response is not None
111- flow_logs_response = humanloop_test_client .logs .list (file_id = flow_response .id , page = 1 , size = 50 )
114+ flow_logs_response = humanloop_client .logs .list (file_id = flow_response .id , page = 1 , size = 50 )
112115 assert flow_logs_response .items is not None and len (flow_logs_response .items ) == 1
113116 flow_log = flow_logs_response .items [0 ]
114117 assert flow_log .error is not None
115118 assert flow_log .output is None
116119
117120 finally :
118- flow_response = humanloop_test_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_flow_log_error" )
121+ flow_response = humanloop_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_flow_log_error" )
119122 if flow_response is not None :
120- humanloop_test_client .flows .delete (id = flow_response .id )
123+ humanloop_client .flows .delete (id = flow_response .id )
121124
122125
123126def test_flow_decorator_populates_output_message (
124- humanloop_test_client : Humanloop ,
127+ get_humanloop_client : GetHumanloopClientFn ,
125128 sdk_test_dir : str ,
126129):
127130 try :
131+ humanloop_client = get_humanloop_client ()
128132
129- @humanloop_test_client .flow (path = f"{ sdk_test_dir } /test_flow_log_output_message" )
133+ @humanloop_client .flow (path = f"{ sdk_test_dir } /test_flow_log_output_message" )
130134 def my_flow (question : str ) -> dict [str , Any ]:
131135 return {"role" : "user" , "content" : question }
132136
133137 assert "france" in my_flow ("What is the capital of the France?" )["content" ].lower ()
134138
135139 time .sleep (5 )
136140
137- flow_response = humanloop_test_client .files .retrieve_by_path (
138- path = f"{ sdk_test_dir } /test_flow_log_output_message"
139- )
141+ flow_response = humanloop_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_flow_log_output_message" )
140142 assert flow_response is not None
141- flow_logs_response = humanloop_test_client .logs .list (file_id = flow_response .id , page = 1 , size = 50 )
143+ flow_logs_response = humanloop_client .logs .list (file_id = flow_response .id , page = 1 , size = 50 )
142144 assert flow_logs_response .items is not None and len (flow_logs_response .items ) == 1
143145 flow_log = flow_logs_response .items [0 ]
144146 assert flow_log .output_message is not None
145147 assert flow_log .output is None
146148 assert flow_log .error is None
147149
148150 finally :
149- flow_response = humanloop_test_client .files .retrieve_by_path (
150- path = f"{ sdk_test_dir } /test_flow_log_output_message"
151- )
151+ flow_response = humanloop_client .files .retrieve_by_path (path = f"{ sdk_test_dir } /test_flow_log_output_message" )
152152 if flow_response is not None :
153- humanloop_test_client .flows .delete (id = flow_response .id )
153+ humanloop_client .flows .delete (id = flow_response .id )
0 commit comments