Skip to content

Commit

Permalink
feat: Add with_history option for chatglm (#8048)
Browse files Browse the repository at this point in the history
In certain 0-shot scenarios, the existing stateful language model can
unintentionally send/accumulate the .history.

This commit adds the "with_history" option to chatglm, allowing users to
control the behavior of .history and prevent unintended accumulation.

Possible reviewers @hwchase17 @baskaryan @mlot

Refer to discussion over this thread:
https://twitter.com/wey_gu/status/1681996149543276545?s=20
  • Loading branch information
wey-gu authored Jul 21, 2023
1 parent 1f3b987 commit cf60cff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@
"\n",
"llm_chain.run(question)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By Default, ChatGLM is statful to keep track of the conversation history and send the accumulated context to the model. To enable stateless mode, we could set ChatGLM.with_history as `False` explicitly."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"llm.with_history = False"
]
}
],
"metadata": {
Expand Down
12 changes: 9 additions & 3 deletions langchain/llms/chatglm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import Any, List, Mapping, Optional

import requests
Expand All @@ -6,6 +7,8 @@
from langchain.llms.base import LLM
from langchain.llms.utils import enforce_stop_tokens

logger = logging.getLogger(__name__)


class ChatGLM(LLM):
"""ChatGLM LLM service.
Expand Down Expand Up @@ -34,6 +37,8 @@ class ChatGLM(LLM):
"""History of the conversation"""
top_p: float = 0.7
"""Top P for nucleus sampling from 0 to 1"""
with_history: bool = True
"""Whether to use history or not"""

@property
def _llm_type(self) -> str:
Expand Down Expand Up @@ -85,15 +90,15 @@ def _call(
payload.update(_model_kwargs)
payload.update(kwargs)

# print("ChatGLM payload:", payload)
logger.debug(f"ChatGLM payload: {payload}")

# call api
try:
response = requests.post(self.endpoint_url, headers=headers, json=payload)
except requests.exceptions.RequestException as e:
raise ValueError(f"Error raised by inference endpoint: {e}")

# print("ChatGLM resp:", response)
logger.debug(f"ChatGLM response: {response}")

if response.status_code != 200:
raise ValueError(f"Failed with response: {response}")
Expand All @@ -119,5 +124,6 @@ def _call(

if stop is not None:
text = enforce_stop_tokens(text, stop)
self.history = self.history + [[None, parsed_response["response"]]]
if self.with_history:
self.history = self.history + [[None, parsed_response["response"]]]
return text

4 comments on commit cf60cff

@mlot
Copy link
Contributor

@mlot mlot commented on cf60cff Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @wey-gu, since it's the recent contributed feature, do you think this (turn with_history=False) should be the default behavior for ChatGLM? if so, i'd like to fix it.

@wey-gu
Copy link
Contributor Author

@wey-gu wey-gu commented on cf60cff Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think so as it seems others are stateless :-P, but was keeping it as True to not break anyone who already implement something on top of it:-P.

And this is a great integration btw, thanks @mlot :)

@mlot
Copy link
Contributor

@mlot mlot commented on cf60cff Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wey-gu it has been just merged in release 235 this week, should not have much impact on existing users :), I will initiate another PR to fix it.

@wey-gu
Copy link
Contributor Author

@wey-gu wey-gu commented on cf60cff Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll be cool :)

Please sign in to comment.