Skip to content

Commit 371b374

Browse files
authored
update to openai > 1 (#529)
* update to openai > 1 * changelog
1 parent 31efb53 commit 371b374

File tree

9 files changed

+404
-280
lines changed

9 files changed

+404
-280
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
Nothing unreleased!
1010

11+
## [0.7.600rc0] - 2023-11-08
12+
13+
### Changed
14+
15+
- Replaced aiohttp with httpx
16+
- Prompt Playground has been updated to work with the new openai release (v1). Including tools.
17+
1118
## [0.7.500] - 2023-11-07
1219

1320
### Added

backend/chainlit/client/cloud.py

+22-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import uuid
22
from typing import Any, Dict, List, Optional, Union
33

4-
import aiohttp
4+
import httpx
55
from chainlit.logger import logger
66

77
from .base import (
@@ -459,41 +459,37 @@ async def upload_element(
459459
if conversation_id:
460460
body["conversationId"] = conversation_id
461461

462-
path = f"/api/upload/file"
462+
path = "/api/upload/file"
463463

464-
async with aiohttp.ClientSession() as session:
465-
async with session.post(
464+
async with httpx.AsyncClient() as client:
465+
response = await client.post(
466466
f"{self.chainlit_server}{path}",
467467
json=body,
468468
headers=self.headers,
469-
) as r:
470-
if not r.ok:
471-
reason = await r.text()
472-
logger.error(f"Failed to sign upload url: {reason}")
473-
return {"object_key": None, "url": None}
474-
json_res = await r.json()
469+
)
470+
if response.status_code != 200:
471+
reason = response.text
472+
logger.error(f"Failed to sign upload url: {reason}")
473+
return {"object_key": None, "url": None}
474+
json_res = response.json()
475475

476476
upload_details = json_res["post"]
477477
object_key = upload_details["fields"]["key"]
478478
signed_url = json_res["signedUrl"]
479479

480-
form_data = aiohttp.FormData()
480+
# Prepare form data
481+
form_data = upload_details["fields"].copy()
482+
form_data["file"] = (id, content, "multipart/form-data")
481483

482-
# Add fields to the form_data
483-
for field_name, field_value in upload_details["fields"].items():
484-
form_data.add_field(field_name, field_value)
485-
486-
# Add file to the form_data
487-
form_data.add_field("file", content, content_type="multipart/form-data")
488-
async with aiohttp.ClientSession() as session:
489-
async with session.post(
484+
async with httpx.AsyncClient() as client:
485+
upload_response = await client.post(
490486
upload_details["url"],
491-
data=form_data,
492-
) as upload_response:
493-
if not upload_response.ok:
494-
reason = await upload_response.text()
495-
logger.error(f"Failed to upload file: {reason}")
496-
return {"object_key": None, "url": None}
497-
487+
files=form_data,
488+
)
489+
try:
490+
upload_response.raise_for_status()
498491
url = f'{upload_details["url"]}/{object_key}'
499492
return {"object_key": object_key, "url": signed_url}
493+
except Exception as e:
494+
logger.error(f"Failed to upload file: {str(e)}")
495+
return {"object_key": None, "url": None}

backend/chainlit/langflow/__init__.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from typing import Dict, Optional, Union
99

10-
import aiohttp
10+
import httpx
1111
from chainlit.telemetry import trace_event
1212

1313

@@ -16,15 +16,12 @@ async def load_flow(schema: Union[Dict, str], tweaks: Optional[Dict] = None):
1616

1717
trace_event("load_langflow")
1818

19-
if type(schema) == str:
20-
async with aiohttp.ClientSession() as session:
21-
async with session.get(
22-
schema,
23-
) as r:
24-
if not r.ok:
25-
reason = await r.text()
26-
raise ValueError(f"Error: {reason}")
27-
schema = await r.json()
19+
if isinstance(schema, str):
20+
async with httpx.AsyncClient() as client:
21+
response = await client.get(schema)
22+
if response.status_code != 200:
23+
raise ValueError(f"Error: {response.text}")
24+
schema = response.json()
2825

2926
flow = load_flow_from_json(flow=schema, tweaks=tweaks)
3027

0 commit comments

Comments
 (0)