2
2
import json
3
3
import ast
4
4
import os
5
+ from aiohttp import ClientSession
5
6
from typing import Dict , List , Optional , Union
6
7
from tenacity import retry , stop_after_attempt , wait_exponential
7
8
23
24
logging .warning ("openai package is not installed" )
24
25
else :
25
26
openai .api_key = os .environ .get ("OPENAI_API_KEY" )
26
- openai .proxy = os .environ .get ("http_proxy" )
27
- if openai .proxy is None :
28
- openai .proxy = os .environ .get ("HTTP_PROXY" )
27
+ # openai.proxy = os.environ.get("http_proxy")
28
+ # if openai.proxy is None:
29
+ # openai.proxy = os.environ.get("HTTP_PROXY")
29
30
if openai .api_key is None :
30
31
logging .warning (
31
32
"OpenAI API key is not set. Please set the environment variable OPENAI_API_KEY"
@@ -114,9 +115,6 @@ def generate_response(
114
115
append_prompt : str = "" ,
115
116
functions : List [dict ] = [],
116
117
) -> LLMResult :
117
- # logger.debug(prepend_prompt)
118
- # logger.debug(history)
119
- # logger.debug(append_prompt)
120
118
messages = self .construct_messages (prepend_prompt , history , append_prompt )
121
119
logger .log_prompt (messages )
122
120
@@ -126,9 +124,6 @@ def generate_response(
126
124
response = openai .ChatCompletion .create (
127
125
messages = messages ,
128
126
functions = functions ,
129
- # function_call="auto",
130
- # function_call={"name": "run_code"},
131
- # stream=True,
132
127
** self .args .dict (),
133
128
)
134
129
if response ["choices" ][0 ]["message" ].get ("function_call" ) is not None :
@@ -180,23 +175,18 @@ async def agenerate_response(
180
175
append_prompt : str = "" ,
181
176
functions : List [dict ] = [],
182
177
) -> LLMResult :
183
- # logger.debug(prepend_prompt)
184
- # logger.debug(history)
185
- # logger.debug(append_prompt)
186
178
messages = self .construct_messages (prepend_prompt , history , append_prompt )
187
179
logger .log_prompt (messages )
188
180
189
181
try :
190
- # Execute function call
191
182
if functions != []:
192
- response = await openai .ChatCompletion .acreate (
193
- messages = messages ,
194
- functions = functions ,
195
- # function_call="auto",
196
- # function_call={"name": "run_code"},
197
- # stream=True,
198
- ** self .args .dict (),
199
- )
183
+ async with ClientSession (trust_env = True ) as session :
184
+ openai .aiosession .set (session )
185
+ response = await openai .ChatCompletion .acreate (
186
+ messages = messages ,
187
+ functions = functions ,
188
+ ** self .args .dict (),
189
+ )
200
190
if response ["choices" ][0 ]["message" ].get ("function_call" ) is not None :
201
191
function_name = response ["choices" ][0 ]["message" ]["function_call" ][
202
192
"name"
@@ -256,10 +246,12 @@ async def agenerate_response(
256
246
)
257
247
258
248
else :
259
- response = await openai .ChatCompletion .acreate (
260
- messages = messages ,
261
- ** self .args .dict (),
262
- )
249
+ async with ClientSession (trust_env = True ) as session :
250
+ openai .aiosession .set (session )
251
+ response = await openai .ChatCompletion .acreate (
252
+ messages = messages ,
253
+ ** self .args .dict (),
254
+ )
263
255
return LLMResult (
264
256
content = response ["choices" ][0 ]["message" ]["content" ],
265
257
send_tokens = response ["usage" ]["prompt_tokens" ],
0 commit comments