Skip to content

Commit

Permalink
Fix Gradio Streaming problems (#39)
Browse files Browse the repository at this point in the history
* fix gradio streaming timeouts and langchain fix dependency

* fix formatting and add dev dependencies

* remove dep deps again

* ➕ add pre-commit to pyproject.toml

📝 update CONTRIBUTING

---------

Co-authored-by: Ajinkya Indulkar <26824103+ajndkr@users.noreply.github.com>
  • Loading branch information
Jan Philipp Harries and ajndkr authored May 13, 2023
1 parent cec8d4a commit 3d04ba4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ poetry install
fastapi-async-langchain uses `pre-commit` to run code checks and tests before every commit. To install the pre-commit hooks, run the following commands:

```bash
pip install pre-commit
pre-commit install
```
28 changes: 25 additions & 3 deletions fastapi_async_langchain/testing/gradio.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,34 @@ def send_query(api_url: str, query: str, chat: list[Any], history: list[Any]):
history.append(query)

payload = {"query": query}
headers = {"accept": "text/event-stream", "Content-Type": "application/json"}
response = requests.post(api_url, headers=headers, json=payload, stream=True)
headers = {
"accept": "text/event-stream",
"Content-Type": "application/json",
"Connection": "keep-alive",
}

try:
response = requests.post(
api_url, headers=headers, json=payload, timeout=60, stream=True
)
response.raise_for_status() # Raise stored HTTPError, if one occurred

except requests.exceptions.HTTPError as httpErr:
print(f"HTTP error occurred: {httpErr}")
return
except requests.exceptions.ConnectionError as connErr:
print(f"Error connecting: {connErr}")
return
except requests.exceptions.Timeout as timeOutErr:
print(f"Timeout error: {timeOutErr}")
return
except requests.exceptions.RequestException as reqErr:
print(f"Something went wrong with the request: {reqErr}")
return

token_counter = 0
partial_words = ""
for chunk in response.iter_content():
for chunk in response.iter_content(chunk_size=1024): # Smaller chunk size
if chunk:
partial_words = partial_words + chunk.decode()
if token_counter == 0:
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ packages = [{include = "fastapi_async_langchain"}]
[tool.poetry.dependencies]
python = "^3.9"
fastapi = "^0.95.1"
langchain = ">=0.0.166"
langchain = ">=0.0.167"
urllib3 = "<=1.26.15" # added due to poetry errors
python-dotenv = "^1.0.0"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.3.1"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down

0 comments on commit 3d04ba4

Please sign in to comment.