Skip to content

Commit

Permalink
fix: #396
Browse files Browse the repository at this point in the history
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
  • Loading branch information
yihong0618 committed Dec 13, 2023
1 parent be73971 commit bce45ff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
10 changes: 8 additions & 2 deletions xiaogpt/bot/chatgptapi_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ async def ask(self, query, **options):
ms = self.get_messages()
ms.append({"role": "user", "content": f"{query}"})
kwargs = {**self.default_options, **options}
async with httpx.AsyncClient(trust_env=True, proxies=self.proxy) as sess:
httpx_kwargs = {}
if self.proxy:
httpx_kwargs["proxies"] = self.proxy
async with httpx.AsyncClient(trust_env=True, **httpx_kwargs) as sess:
client = self._make_openai_client(sess)
try:
completion = await client.chat.completions.create(messages=ms, **kwargs)
Expand All @@ -65,7 +68,10 @@ async def ask_stream(self, query, **options):
ms = self.get_messages()
ms.append({"role": "user", "content": f"{query}"})
kwargs = {**self.default_options, **options}
async with httpx.AsyncClient(trust_env=True, proxies=self.proxy) as sess:
httpx_kwargs = {}
if self.proxy:
httpx_kwargs["proxies"] = self.proxy
async with httpx.AsyncClient(trust_env=True, **httpx_kwargs) as sess:
client = self._make_openai_client(sess)
try:
completion = await client.chat.completions.create(
Expand Down
10 changes: 8 additions & 2 deletions xiaogpt/bot/gpt3_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ async def ask(self, query, **options):
"top_p": 1,
**options,
}
async with httpx.AsyncClient(trust_env=True, proxies=self.proxy) as sess:
httpx_kwargs = {}
if self.config.proxy:
httpx_kwargs["proxies"] = self.config.proxy
async with httpx.AsyncClient(trust_env=True, **httpx_kwargs) as sess:
client = openai.AsyncOpenAI(
api_key=self.openai_key, http_client=sess, base_url=self.api_base
)
Expand All @@ -56,7 +59,10 @@ async def ask_stream(self, query, **options):
"stream": True,
**options,
}
async with httpx.AsyncClient(trust_env=True, proxies=self.proxy) as sess:
httpx_kwargs = {}
if self.config.proxy:
httpx_kwargs["proxies"] = self.config.proxy
async with httpx.AsyncClient(trust_env=True, **httpx_kwargs) as sess:
client = openai.AsyncOpenAI(
api_key=self.openai_key, http_client=sess, base_url=self.api_base
)
Expand Down
5 changes: 4 additions & 1 deletion xiaogpt/tts/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ async def make_audio_file(self, query: str, text: str) -> tuple[Path, float]:
output_file = tempfile.NamedTemporaryFile(
suffix=".mp3", mode="wb", delete=False, dir=self.dirname.name
)
async with httpx.AsyncClient(trust_env=True, proxies=self.config.proxy) as sess:
httpx_kwargs = {}
if self.config.proxy:
httpx_kwargs["proxies"] = self.config.proxy
async with httpx.AsyncClient(trust_env=True, **httpx_kwargs) as sess:
client = self._make_openai_client(sess)

resp = await client.audio.speech.create(
Expand Down

0 comments on commit bce45ff

Please sign in to comment.