Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PaiEasChatEndpoint._call_eas should return bytes type instead of str type #20453

Closed
5 tasks done
marinelay opened this issue Apr 15, 2024 · 0 comments
Closed
5 tasks done
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@marinelay
Copy link

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

from langchain_community.chat_models import PaiEasChatEndpoint
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template("tell me a short joke about {topic}")
prompt_value = prompt.invoke("test_message")

eas_chat_endpoint = PaiEasChatEndpoint(
    eas_service_url="your_service_url",
    eas_service_token="your_service_token"
)
eas_chat_endpoint._call(prompt_value.messages)

It is just example code that has a potential error, so I'll explain why it's a possible type error in the description.

Error Message and Stack Trace (if applicable)

No response

Description

def _call(
self,
messages: List[BaseMessage],
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
params = self._invocation_params(stop, **kwargs)
request_payload = self.format_request_payload(messages, **params)
response_payload = self._call_eas(request_payload)
generated_text = self._format_response_payload(response_payload, params["stop"])
if run_manager:
run_manager.on_llm_new_token(generated_text)
return generated_text
def _call_eas(self, query_body: dict) -> Any:
"""Generate text from the eas service."""
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": f"{self.eas_service_token}",
}
# make request
response = requests.post(
self.eas_service_url, headers=headers, json=query_body, timeout=self.timeout
)
if response.status_code != 200:
raise Exception(
f"Request failed with status code {response.status_code}"
f" and message {response.text}"
)
return response.text

In the method PaiEasChatEndpoint._call at line 203, self._call_eas(request_payload) returns str type because it returns response.text at line 230 (https://requests.readthedocs.io/en/latest/api/#requests.Response.text).
Then, first argument of _format_response_payload can be str type at line 204, resulting in type mismatch.

def _format_response_payload(
self, output: bytes, stop_sequences: Optional[List[str]]
) -> str:
"""Formats response"""
try:
text = json.loads(output)["response"]
if stop_sequences:
text = enforce_stop_tokens(text, stop_sequences)
return text
except Exception as e:
if isinstance(e, json.decoder.JSONDecodeError):
return output.decode("utf-8")
raise e

Moreover, if JSONDecodeError occurs in this method, then attribute error occurs at line 178 (AttributeError: 'str' object has no attribute 'decode') because output variable is str type.
I think PaiEasChatEndpoint._call_eas should be fixed to return bytes type.
If I'm mistaken, I'd appreciate it if you could let me know.

System Info

System Information

OS: Linux
OS Version: #190-Ubuntu SMP Fri Feb 2 23:24:22 UTC 2024
Python Version: 3.9.18 (main, Feb 13 2024, 14:37:08)
[GCC 9.4.0]

Package Information

langchain_core: 0.1.42
langchain: 0.1.16
langchain_community: 0.0.32
langsmith: 0.1.47
langchain_text_splitters: 0.0.1

Packages not installed (Not Necessarily a Problem)

The following packages were not found:

langgraph
langserve

@dosubot dosubot bot added the 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature label Apr 15, 2024
garylin2099 added a commit to garylin2099/langchain that referenced this issue May 10, 2024
garylin2099 added a commit to garylin2099/langchain that referenced this issue May 10, 2024
Justin-ZL added a commit to Justin-ZL/langchain that referenced this issue Jul 9, 2024
seeker-jie pushed a commit to seeker-jie/langchain that referenced this issue Jul 11, 2024
@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Jul 15, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Jul 22, 2024
yulin-jin pushed a commit to yulin-jin/langchain that referenced this issue Aug 20, 2024
xiaoxiaoimg pushed a commit to xiaoxiaoimg/langchain that referenced this issue Aug 20, 2024
xiaoxiaoimg pushed a commit to xiaoxiaoimg/langchain that referenced this issue Aug 21, 2024
xiaoxiaoimg pushed a commit to xiaoxiaoimg/langchain that referenced this issue Aug 21, 2024
xiaoxiaoimg pushed a commit to xiaoxiaoimg/langchain that referenced this issue Aug 22, 2024
xiaoxiaoimg pushed a commit to xiaoxiaoimg/langchain that referenced this issue Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant