2023-05-31 00:00:25 +08:00
|
|
|
from abc import ABC
|
2023-07-12 23:09:28 +08:00
|
|
|
from langchain.chains.base import Chain
|
2023-07-16 01:29:51 +08:00
|
|
|
from typing import (
|
|
|
|
|
Any, Dict, List, Optional, Generator, Collection, Set,
|
|
|
|
|
Callable,
|
|
|
|
|
Tuple,
|
|
|
|
|
Union)
|
|
|
|
|
|
2023-05-31 00:00:25 +08:00
|
|
|
from models.loader import LoaderCheckPoint
|
2023-07-12 23:09:28 +08:00
|
|
|
from langchain.callbacks.manager import CallbackManagerForChainRun
|
|
|
|
|
from models.base import (BaseAnswer,
|
|
|
|
|
RemoteRpcModel,
|
|
|
|
|
AnswerResult,
|
|
|
|
|
AnswerResultStream,
|
|
|
|
|
AnswerResultQueueSentinelTokenListenerQueue)
|
2023-07-16 01:29:51 +08:00
|
|
|
from tenacity import (
|
|
|
|
|
before_sleep_log,
|
|
|
|
|
retry,
|
|
|
|
|
retry_if_exception_type,
|
|
|
|
|
stop_after_attempt,
|
|
|
|
|
wait_exponential,
|
|
|
|
|
)
|
|
|
|
|
from pydantic import Extra, Field, root_validator
|
|
|
|
|
|
|
|
|
|
from openai import (
|
|
|
|
|
ChatCompletion
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
import openai
|
|
|
|
|
import logging
|
2023-07-12 23:09:28 +08:00
|
|
|
import torch
|
|
|
|
|
import transformers
|
2023-05-31 00:00:25 +08:00
|
|
|
|
2023-07-16 01:29:51 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2023-05-31 00:00:25 +08:00
|
|
|
|
|
|
|
|
def _build_message_template() -> Dict[str, str]:
|
|
|
|
|
"""
|
|
|
|
|
:return: 结构
|
|
|
|
|
"""
|
|
|
|
|
return {
|
|
|
|
|
"role": "",
|
|
|
|
|
"content": "",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 23:09:28 +08:00
|
|
|
# 将历史对话数组转换为文本格式
|
|
|
|
|
def build_message_list(query, history: List[List[str]]) -> Collection[Dict[str, str]]:
|
|
|
|
|
build_messages: Collection[Dict[str, str]] = []
|
2023-07-16 01:29:51 +08:00
|
|
|
|
|
|
|
|
system_build_message = _build_message_template()
|
|
|
|
|
system_build_message['role'] = 'system'
|
|
|
|
|
system_build_message['content'] = "You are a helpful assistant."
|
|
|
|
|
build_messages.append(system_build_message)
|
2023-07-16 02:17:52 +08:00
|
|
|
if history:
|
|
|
|
|
for i, (user, assistant) in enumerate(history):
|
|
|
|
|
if user:
|
|
|
|
|
|
|
|
|
|
user_build_message = _build_message_template()
|
|
|
|
|
user_build_message['role'] = 'user'
|
|
|
|
|
user_build_message['content'] = user
|
|
|
|
|
build_messages.append(user_build_message)
|
|
|
|
|
|
|
|
|
|
if not assistant:
|
|
|
|
|
raise RuntimeError("历史数据结构不正确")
|
|
|
|
|
system_build_message = _build_message_template()
|
|
|
|
|
system_build_message['role'] = 'assistant'
|
|
|
|
|
system_build_message['content'] = assistant
|
|
|
|
|
build_messages.append(system_build_message)
|
2023-07-12 23:09:28 +08:00
|
|
|
|
|
|
|
|
user_build_message = _build_message_template()
|
|
|
|
|
user_build_message['role'] = 'user'
|
|
|
|
|
user_build_message['content'] = query
|
|
|
|
|
build_messages.append(user_build_message)
|
|
|
|
|
return build_messages
|
|
|
|
|
|
2023-07-11 19:36:50 +08:00
|
|
|
|
2023-07-12 23:09:28 +08:00
|
|
|
class FastChatOpenAILLMChain(RemoteRpcModel, Chain, ABC):
|
2023-07-16 01:29:51 +08:00
|
|
|
client: Any
|
|
|
|
|
"""Timeout for requests to OpenAI completion API. Default is 600 seconds."""
|
|
|
|
|
max_retries: int = 6
|
2023-05-31 00:00:25 +08:00
|
|
|
api_base_url: str = "http://localhost:8000/v1"
|
|
|
|
|
model_name: str = "chatglm-6b"
|
|
|
|
|
max_token: int = 10000
|
|
|
|
|
temperature: float = 0.01
|
|
|
|
|
top_p = 0.9
|
|
|
|
|
checkPoint: LoaderCheckPoint = None
|
2023-07-12 23:09:28 +08:00
|
|
|
# history = []
|
2023-05-31 00:00:25 +08:00
|
|
|
history_len: int = 10
|
2023-07-11 19:36:50 +08:00
|
|
|
api_key: str = ""
|
|
|
|
|
|
2023-07-12 23:09:28 +08:00
|
|
|
streaming_key: str = "streaming" #: :meta private:
|
|
|
|
|
history_key: str = "history" #: :meta private:
|
|
|
|
|
prompt_key: str = "prompt" #: :meta private:
|
|
|
|
|
output_key: str = "answer_result_stream" #: :meta private:
|
|
|
|
|
|
|
|
|
|
def __init__(self,
|
2023-07-11 19:36:50 +08:00
|
|
|
checkPoint: LoaderCheckPoint = None,
|
2023-07-12 23:09:28 +08:00
|
|
|
# api_base_url:str="http://localhost:8000/v1",
|
|
|
|
|
# model_name:str="chatglm-6b",
|
|
|
|
|
# api_key:str=""
|
2023-07-11 19:36:50 +08:00
|
|
|
):
|
2023-05-31 00:00:25 +08:00
|
|
|
super().__init__()
|
|
|
|
|
self.checkPoint = checkPoint
|
|
|
|
|
|
|
|
|
|
@property
|
2023-07-12 23:09:28 +08:00
|
|
|
def _chain_type(self) -> str:
|
|
|
|
|
return "LLamaLLMChain"
|
2023-05-31 00:00:25 +08:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def _check_point(self) -> LoaderCheckPoint:
|
|
|
|
|
return self.checkPoint
|
|
|
|
|
|
|
|
|
|
@property
|
2023-07-12 23:09:28 +08:00
|
|
|
def input_keys(self) -> List[str]:
|
|
|
|
|
"""Will be whatever keys the prompt expects.
|
|
|
|
|
|
|
|
|
|
:meta private:
|
|
|
|
|
"""
|
|
|
|
|
return [self.prompt_key]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def output_keys(self) -> List[str]:
|
|
|
|
|
"""Will always return text key.
|
2023-05-31 00:00:25 +08:00
|
|
|
|
2023-07-12 23:09:28 +08:00
|
|
|
:meta private:
|
|
|
|
|
"""
|
|
|
|
|
return [self.output_key]
|
2023-05-31 00:00:25 +08:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def _api_key(self) -> str:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def _api_base_url(self) -> str:
|
|
|
|
|
return self.api_base_url
|
|
|
|
|
|
|
|
|
|
def set_api_key(self, api_key: str):
|
2023-07-11 19:36:50 +08:00
|
|
|
self.api_key = api_key
|
2023-05-31 00:00:25 +08:00
|
|
|
|
|
|
|
|
def set_api_base_url(self, api_base_url: str):
|
|
|
|
|
self.api_base_url = api_base_url
|
|
|
|
|
|
|
|
|
|
def call_model_name(self, model_name):
|
|
|
|
|
self.model_name = model_name
|
|
|
|
|
|
2023-07-16 01:29:51 +08:00
|
|
|
def _create_retry_decorator(self) -> Callable[[Any], Any]:
|
|
|
|
|
min_seconds = 1
|
|
|
|
|
max_seconds = 60
|
|
|
|
|
# Wait 2^x * 1 second between each retry starting with
|
|
|
|
|
# 4 seconds, then up to 10 seconds, then 10 seconds afterwards
|
|
|
|
|
return retry(
|
|
|
|
|
reraise=True,
|
|
|
|
|
stop=stop_after_attempt(self.max_retries),
|
|
|
|
|
wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds),
|
|
|
|
|
retry=(
|
|
|
|
|
retry_if_exception_type(openai.error.Timeout)
|
|
|
|
|
| retry_if_exception_type(openai.error.APIError)
|
|
|
|
|
| retry_if_exception_type(openai.error.APIConnectionError)
|
|
|
|
|
| retry_if_exception_type(openai.error.RateLimitError)
|
|
|
|
|
| retry_if_exception_type(openai.error.ServiceUnavailableError)
|
|
|
|
|
),
|
|
|
|
|
before_sleep=before_sleep_log(logger, logging.WARNING),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def completion_with_retry(self, **kwargs: Any) -> Any:
|
|
|
|
|
"""Use tenacity to retry the completion call."""
|
|
|
|
|
retry_decorator = self._create_retry_decorator()
|
|
|
|
|
|
|
|
|
|
@retry_decorator
|
|
|
|
|
def _completion_with_retry(**kwargs: Any) -> Any:
|
|
|
|
|
return self.client.create(**kwargs)
|
|
|
|
|
|
|
|
|
|
return _completion_with_retry(**kwargs)
|
|
|
|
|
|
2023-07-12 23:09:28 +08:00
|
|
|
def _call(
|
|
|
|
|
self,
|
|
|
|
|
inputs: Dict[str, Any],
|
|
|
|
|
run_manager: Optional[CallbackManagerForChainRun] = None,
|
|
|
|
|
) -> Dict[str, Generator]:
|
|
|
|
|
generator = self.generatorAnswer(inputs=inputs, run_manager=run_manager)
|
|
|
|
|
return {self.output_key: generator}
|
|
|
|
|
|
|
|
|
|
def _generate_answer(self,
|
|
|
|
|
inputs: Dict[str, Any],
|
|
|
|
|
run_manager: Optional[CallbackManagerForChainRun] = None,
|
|
|
|
|
generate_with_callback: AnswerResultStream = None) -> None:
|
|
|
|
|
|
2023-07-16 02:17:52 +08:00
|
|
|
history = inputs.get(self.history_key, [])
|
|
|
|
|
streaming = inputs.get(self.streaming_key, False)
|
2023-07-12 23:09:28 +08:00
|
|
|
prompt = inputs[self.prompt_key]
|
2023-07-16 02:17:52 +08:00
|
|
|
stop = inputs.get("stop", "stop")
|
2023-06-10 22:14:50 +08:00
|
|
|
print(f"__call:{prompt}")
|
|
|
|
|
try:
|
2023-05-31 00:00:25 +08:00
|
|
|
|
|
|
|
|
# Not support yet
|
2023-07-11 19:36:50 +08:00
|
|
|
# openai.api_key = "EMPTY"
|
|
|
|
|
openai.api_key = self.api_key
|
2023-05-31 00:00:25 +08:00
|
|
|
openai.api_base = self.api_base_url
|
2023-07-16 01:29:51 +08:00
|
|
|
self.client = openai.ChatCompletion
|
|
|
|
|
except AttributeError:
|
2023-05-31 00:00:25 +08:00
|
|
|
raise ValueError(
|
2023-07-16 01:29:51 +08:00
|
|
|
"`openai` has no `ChatCompletion` attribute, this is likely "
|
|
|
|
|
"due to an old version of the openai package. Try upgrading it "
|
|
|
|
|
"with `pip install --upgrade openai`."
|
2023-05-31 00:00:25 +08:00
|
|
|
)
|
2023-07-16 01:29:51 +08:00
|
|
|
msg = build_message_list(prompt, history=history)
|
|
|
|
|
|
|
|
|
|
if streaming:
|
|
|
|
|
params = {"stream": streaming,
|
|
|
|
|
"model": self.model_name,
|
|
|
|
|
"stop": stop}
|
2023-07-16 02:17:52 +08:00
|
|
|
out_str = ""
|
2023-07-16 01:29:51 +08:00
|
|
|
for stream_resp in self.completion_with_retry(
|
|
|
|
|
messages=msg,
|
|
|
|
|
**params
|
|
|
|
|
):
|
|
|
|
|
role = stream_resp["choices"][0]["delta"].get("role", "")
|
|
|
|
|
token = stream_resp["choices"][0]["delta"].get("content", "")
|
2023-07-16 02:17:52 +08:00
|
|
|
out_str += token
|
|
|
|
|
history[-1] = [prompt, out_str]
|
2023-07-16 01:29:51 +08:00
|
|
|
answer_result = AnswerResult()
|
|
|
|
|
answer_result.history = history
|
2023-07-16 02:17:52 +08:00
|
|
|
answer_result.llm_output = {"answer": out_str}
|
2023-07-16 01:29:51 +08:00
|
|
|
generate_with_callback(answer_result)
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
params = {"stream": streaming,
|
|
|
|
|
"model": self.model_name,
|
|
|
|
|
"stop": stop}
|
|
|
|
|
response = self.completion_with_retry(
|
|
|
|
|
messages=msg,
|
|
|
|
|
**params
|
|
|
|
|
)
|
|
|
|
|
role = response["choices"][0]["message"].get("role", "")
|
|
|
|
|
content = response["choices"][0]["message"].get("content", "")
|
|
|
|
|
history += [[prompt, content]]
|
|
|
|
|
answer_result = AnswerResult()
|
|
|
|
|
answer_result.history = history
|
|
|
|
|
answer_result.llm_output = {"answer": content}
|
|
|
|
|
generate_with_callback(answer_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
chain = FastChatOpenAILLMChain()
|
|
|
|
|
|
2023-07-19 10:57:09 +08:00
|
|
|
chain.set_api_key("EMPTY")
|
2023-07-16 02:17:52 +08:00
|
|
|
# chain.set_api_base_url("https://api.openai.com/v1")
|
|
|
|
|
# chain.call_model_name("gpt-3.5-turbo")
|
2023-07-16 01:29:51 +08:00
|
|
|
|
2023-07-16 02:17:52 +08:00
|
|
|
answer_result_stream_result = chain({"streaming": True,
|
2023-07-16 01:29:51 +08:00
|
|
|
"prompt": "你好",
|
|
|
|
|
"history": []
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
for answer_result in answer_result_stream_result['answer_result_stream']:
|
|
|
|
|
resp = answer_result.llm_output["answer"]
|
|
|
|
|
print(resp)
|