Skip to content
Prev Previous commit
Next Next commit
apply Black formatting
  • Loading branch information
AlexZhangji committed Mar 25, 2023
commit cd284546930523620b868ab2404227d77631a0af
34 changes: 22 additions & 12 deletions example_with_langchain_and_vectorstore/chatglm_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,41 @@

"""ChatGLM_G is a wrapper around the ChatGLM model to fit LangChain framework. May not be an optimal implementation"""


class ChatGLM_G(LLM):

tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True).half().cuda()
tokenizer = AutoTokenizer.from_pretrained(
"THUDM/chatglm-6b-int4", trust_remote_code=True
)
model = (
AutoModel.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True)
.half()
.cuda()
)
history = []

@property
def _llm_type(self) -> str:
return "ChatGLM_G"

def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response, updated_history = self.model.chat(self.tokenizer, prompt, history=self.history)
print("ChatGLM: prompt: ", prompt)
print("ChatGLM: response: ", response)
response, updated_history = self.model.chat(
self.tokenizer, prompt, history=self.history, max_length=10000
)
print("history: ", self.history)
if stop is not None:
response = enforce_stop_tokens(response, stop)
self.history = updated_history
return response

def __call__(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response, updated_history = self.model.chat(self.tokenizer, prompt, history=self.history)
print("ChatGLM: prompt: ", prompt)
print("ChatGLM: response: ", response)

def __call__(self, prompt: str, stop: Optional[List[str]] = None) -> str:
response, updated_history = self.model.chat(
self.tokenizer, prompt, history=self.history, max_length=10000
)
print("history: ", self.history)

if stop is not None:
response = enforce_stop_tokens(response, stop)
self.history = updated_history
return response

return response