update zhipuai notebook (#20595)

fix timeout issue
fix zhipuai usecase notebookbook

Thank you for contributing to LangChain!

- [ ] **PR title**: "package: description"
- Where "package" is whichever of langchain, community, core,
experimental, etc. is being modified. Use "docs: ..." for purely docs
changes, "templates: ..." for template changes, "infra: ..." for CI
changes.
  - Example: "community: add foobar LLM"


- [ ] **PR message**: ***Delete this entire checklist*** and replace
with
    - **Description:** a description of the change
    - **Issue:** the issue # it fixes, if applicable
    - **Dependencies:** any dependencies required for this change
- **Twitter handle:** if your PR gets announced, and you'd like a
mention, we'll gladly shout you out!


- [ ] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [ ] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, efriis, eyurtsev, hwchase17.
This commit is contained in:
zR
2024-04-18 22:12:12 +08:00
committed by GitHub
parent 9c175bc618
commit 9c1d7f2405
2 changed files with 64 additions and 95 deletions

View File

@@ -42,7 +42,6 @@ ZHIPUAI_API_BASE = "https://open.bigmodel.cn/api/paas/v4/chat/completions"
@contextmanager
def connect_sse(client: Any, method: str, url: str, **kwargs: Any) -> Iterator:
"""Connect to a server-sent event stream."""
from httpx_sse import EventSource
with client.stream(method, url, **kwargs) as response:
@@ -53,7 +52,6 @@ def connect_sse(client: Any, method: str, url: str, **kwargs: Any) -> Iterator:
async def aconnect_sse(
client: Any, method: str, url: str, **kwargs: Any
) -> AsyncIterator:
"""Async connect to a server-sent event stream."""
from httpx_sse import EventSource
async with client.stream(method, url, **kwargs) as response:
@@ -317,7 +315,7 @@ class ChatZhipuAI(BaseChatModel):
}
import httpx
with httpx.Client(headers=headers) as client:
with httpx.Client(headers=headers, timeout=60) as client:
response = client.post(self.zhipuai_api_base, json=payload)
response.raise_for_status()
return self._create_chat_result(response.json())
@@ -344,7 +342,7 @@ class ChatZhipuAI(BaseChatModel):
default_chunk_class = AIMessageChunk
import httpx
with httpx.Client(headers=headers) as client:
with httpx.Client(headers=headers, timeout=60) as client:
with connect_sse(
client, "POST", self.zhipuai_api_base, json=payload
) as event_source:
@@ -402,7 +400,7 @@ class ChatZhipuAI(BaseChatModel):
}
import httpx
async with httpx.AsyncClient(headers=headers) as client:
async with httpx.AsyncClient(headers=headers, timeout=60) as client:
response = await client.post(self.zhipuai_api_base, json=payload)
response.raise_for_status()
return self._create_chat_result(response.json())
@@ -428,7 +426,7 @@ class ChatZhipuAI(BaseChatModel):
default_chunk_class = AIMessageChunk
import httpx
async with httpx.AsyncClient(headers=headers) as client:
async with httpx.AsyncClient(headers=headers, timeout=60) as client:
async with aconnect_sse(
client, "POST", self.zhipuai_api_base, json=payload
) as event_source: