bisheng-langchain 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. bisheng_langchain/__init__.py +0 -0
  2. bisheng_langchain/chains/__init__.py +5 -0
  3. bisheng_langchain/chains/combine_documents/__init__.py +0 -0
  4. bisheng_langchain/chains/combine_documents/stuff.py +56 -0
  5. bisheng_langchain/chains/question_answering/__init__.py +240 -0
  6. bisheng_langchain/chains/retrieval_qa/__init__.py +0 -0
  7. bisheng_langchain/chains/retrieval_qa/base.py +89 -0
  8. bisheng_langchain/chat_models/__init__.py +11 -0
  9. bisheng_langchain/chat_models/host_llm.py +409 -0
  10. bisheng_langchain/chat_models/interface/__init__.py +10 -0
  11. bisheng_langchain/chat_models/interface/minimax.py +123 -0
  12. bisheng_langchain/chat_models/interface/openai.py +68 -0
  13. bisheng_langchain/chat_models/interface/types.py +61 -0
  14. bisheng_langchain/chat_models/interface/utils.py +5 -0
  15. bisheng_langchain/chat_models/interface/wenxin.py +114 -0
  16. bisheng_langchain/chat_models/interface/xunfei.py +233 -0
  17. bisheng_langchain/chat_models/interface/zhipuai.py +81 -0
  18. bisheng_langchain/chat_models/minimax.py +354 -0
  19. bisheng_langchain/chat_models/proxy_llm.py +354 -0
  20. bisheng_langchain/chat_models/wenxin.py +349 -0
  21. bisheng_langchain/chat_models/xunfeiai.py +355 -0
  22. bisheng_langchain/chat_models/zhipuai.py +379 -0
  23. bisheng_langchain/document_loaders/__init__.py +3 -0
  24. bisheng_langchain/document_loaders/elem_html.py +0 -0
  25. bisheng_langchain/document_loaders/elem_image.py +0 -0
  26. bisheng_langchain/document_loaders/elem_pdf.py +655 -0
  27. bisheng_langchain/document_loaders/parsers/__init__.py +5 -0
  28. bisheng_langchain/document_loaders/parsers/image.py +28 -0
  29. bisheng_langchain/document_loaders/parsers/test_image.py +286 -0
  30. bisheng_langchain/embeddings/__init__.py +7 -0
  31. bisheng_langchain/embeddings/host_embedding.py +133 -0
  32. bisheng_langchain/embeddings/interface/__init__.py +3 -0
  33. bisheng_langchain/embeddings/interface/types.py +23 -0
  34. bisheng_langchain/embeddings/interface/wenxin.py +86 -0
  35. bisheng_langchain/embeddings/wenxin.py +139 -0
  36. bisheng_langchain/vectorstores/__init__.py +3 -0
  37. bisheng_langchain/vectorstores/elastic_keywords_search.py +284 -0
  38. bisheng_langchain-0.0.1.dist-info/METADATA +64 -0
  39. bisheng_langchain-0.0.1.dist-info/RECORD +41 -0
  40. bisheng_langchain-0.0.1.dist-info/WHEEL +5 -0
  41. bisheng_langchain-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,354 @@
1
+ """proxy llm chat wrapper."""
2
+ from __future__ import annotations
3
+
4
+ import logging
5
+ import sys
6
+ from typing import TYPE_CHECKING, Any, Callable, Dict, List, Mapping, Optional, Tuple, Union
7
+
8
+ # import requests
9
+ from langchain.callbacks.manager import AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun
10
+ from langchain.chat_models.base import BaseChatModel
11
+ from langchain.schema import ChatGeneration, ChatResult
12
+ from langchain.schema.messages import (AIMessage, BaseMessage, ChatMessage, FunctionMessage,
13
+ HumanMessage, SystemMessage)
14
+ from langchain.utils import get_from_dict_or_env
15
+ from pydantic import Field, root_validator
16
+ from tenacity import (before_sleep_log, retry, retry_if_exception_type, stop_after_attempt,
17
+ wait_exponential)
18
+
19
+ from .interface import MinimaxChatCompletion
20
+ from .interface.types import ChatInput
21
+
22
+ if TYPE_CHECKING:
23
+ import tiktoken
24
+
25
+ logger = logging.getLogger(__name__)
26
+
27
+
28
+ def _import_tiktoken() -> Any:
29
+ try:
30
+ import tiktoken
31
+ except ImportError:
32
+ raise ValueError('Could not import tiktoken python package. '
33
+ 'This is needed in order to calculate get_token_ids. '
34
+ 'Please install it with `pip install tiktoken`.')
35
+ return tiktoken
36
+
37
+
38
+ def _create_retry_decorator(llm: ChatMinimaxAI) -> Callable[[Any], Any]:
39
+
40
+ min_seconds = 1
41
+ max_seconds = 20
42
+ # Wait 2^x * 1 second between each retry starting with
43
+ # 4 seconds, then up to 10 seconds, then 10 seconds afterwards
44
+ return retry(
45
+ reraise=True,
46
+ stop=stop_after_attempt(llm.max_retries),
47
+ wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds),
48
+ retry=(retry_if_exception_type(Exception)),
49
+ before_sleep=before_sleep_log(logger, logging.WARNING),
50
+ )
51
+
52
+
53
+ def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:
54
+ role = _dict['role']
55
+ if role == 'user':
56
+ return HumanMessage(content=_dict['content'])
57
+ elif role == 'assistant':
58
+ content = _dict[
59
+ 'content'] or '' # OpenAI returns None for tool invocations
60
+ if _dict.get('function_call'):
61
+ additional_kwargs = {'function_call': dict(_dict['function_call'])}
62
+ else:
63
+ additional_kwargs = {}
64
+ return AIMessage(content=content, additional_kwargs=additional_kwargs)
65
+ elif role == 'system':
66
+ return SystemMessage(content=_dict['content'])
67
+ elif role == 'function':
68
+ return FunctionMessage(content=_dict['content'], name=_dict['name'])
69
+ else:
70
+ return ChatMessage(content=_dict['content'], role=role)
71
+
72
+
73
+ def _convert_message_to_dict(message: BaseMessage) -> dict:
74
+ if isinstance(message, ChatMessage):
75
+ message_dict = {'role': message.role, 'content': message.content}
76
+ elif isinstance(message, HumanMessage):
77
+ message_dict = {'role': 'user', 'content': message.content}
78
+ elif isinstance(message, AIMessage):
79
+ message_dict = {'role': 'assistant', 'content': message.content}
80
+ if 'function_call' in message.additional_kwargs:
81
+ message_dict['function_call'] = message.additional_kwargs[
82
+ 'function_call']
83
+ elif isinstance(message, SystemMessage):
84
+ message_dict = {'role': 'system', 'content': message.content}
85
+ elif isinstance(message, FunctionMessage):
86
+ message_dict = {
87
+ 'role': 'function',
88
+ 'content': message.content,
89
+ 'name': message.name,
90
+ }
91
+ else:
92
+ raise ValueError(f'Got unknown type {message}')
93
+ if 'name' in message.additional_kwargs:
94
+ message_dict['name'] = message.additional_kwargs['name']
95
+ return message_dict
96
+
97
+
98
+ class ChatMinimaxAI(BaseChatModel):
99
+ """Wrapper around proxy Chat large language models.
100
+
101
+ To use, the environment variable ``ELEMAI_API_KEY`` set with your API key.
102
+
103
+ Example:
104
+ .. code-block:: python
105
+
106
+ from bisheng_langchain.chat_models import ChatMinimaxAI
107
+ chat_miniamaxai = ChatMinimaxAI(model_name="abab5.5-chat")
108
+ """
109
+
110
+ client: Optional[Any] #: :meta private:
111
+ """Model name to use."""
112
+ model_name: str = Field('abab5.5-chat', alias='model')
113
+
114
+ temperature: float = 0.9
115
+ top_p: float = 0.95
116
+ """What sampling temperature to use."""
117
+ model_kwargs: Optional[Dict[str, Any]] = Field(default_factory=dict)
118
+ """Holds any model parameters valid for `create` call not explicitly specified."""
119
+ minimaxai_api_key: Optional[str] = None
120
+ minimaxai_group_id: Optional[str] = None
121
+
122
+ headers: Optional[Dict[str, str]] = Field(default_factory=dict)
123
+
124
+ request_timeout: Optional[Union[float, Tuple[float, float]]] = None
125
+ """Timeout for requests to OpenAI completion API. Default is 600 seconds."""
126
+ max_retries: Optional[int] = 6
127
+ """Maximum number of retries to make when generating."""
128
+ streaming: Optional[bool] = False
129
+ """Whether to stream the results or not."""
130
+ n: Optional[int] = 1
131
+ """Number of chat completions to generate for each prompt."""
132
+ max_tokens: Optional[int] = None
133
+ """Maximum number of tokens to generate."""
134
+ tiktoken_model_name: Optional[str] = None
135
+ """The model name to pass to tiktoken when using this class.
136
+ Tiktoken is used to count the number of tokens in documents to constrain
137
+ them to be under a certain limit. By default, when set to None, this will
138
+ be the same as the embedding model name. However, there are some cases
139
+ where you may want to use this Embedding class with a model name not
140
+ supported by tiktoken. This can include when using Azure embeddings or
141
+ when using one of the many model providers that expose an OpenAI-like
142
+ API but with different models. In those cases, in order to avoid erroring
143
+ when tiktoken is called, you can specify a model name to use here."""
144
+
145
+ verbose: Optional[bool] = False
146
+
147
+ class Config:
148
+ """Configuration for this pydantic object."""
149
+
150
+ allow_population_by_field_name = True
151
+
152
+ @root_validator()
153
+ def validate_environment(cls, values: Dict) -> Dict:
154
+ """Validate that api key and python package exists in environment."""
155
+ values['minimaxai_api_key'] = get_from_dict_or_env(
156
+ values, 'minimaxai_api_key', 'MINIMAXAI_API_KEY')
157
+
158
+ values['minimaxai_group_id'] = get_from_dict_or_env(
159
+ values, 'minimaxai_group_id', 'MINIMAXAI_GROUP_ID')
160
+
161
+ api_key = values['minimaxai_api_key']
162
+ group_id = values['minimaxai_group_id']
163
+ try:
164
+ values['client'] = MinimaxChatCompletion(group_id, api_key)
165
+ except AttributeError:
166
+ raise ValueError(
167
+ 'Try upgrading it with `pip install --upgrade requests`.')
168
+ return values
169
+
170
+ @property
171
+ def _default_params(self) -> Dict[str, Any]:
172
+ """Get the default parameters for calling ChatMinimaxAI API."""
173
+ return {
174
+ 'model': self.model_name,
175
+ 'temperature': self.temperature,
176
+ 'top_p': self.top_p,
177
+ 'max_tokens': self.max_tokens,
178
+ **self.model_kwargs,
179
+ }
180
+
181
+ def completion_with_retry(self, **kwargs: Any) -> Any:
182
+ retry_decorator = _create_retry_decorator(self)
183
+
184
+ @retry_decorator
185
+ def _completion_with_retry(**kwargs: Any) -> Any:
186
+ messages = kwargs.get('messages')
187
+ temperature = kwargs.get('temperature')
188
+ top_p = kwargs.get('top_p')
189
+ max_tokens = kwargs.get('max_tokens')
190
+ params = {
191
+ 'messages': messages,
192
+ 'model': self.model_name,
193
+ 'top_p': top_p,
194
+ 'temperature': temperature,
195
+ 'max_tokens': max_tokens
196
+ }
197
+ return self.client(ChatInput.parse_obj(params),
198
+ self.verbose).dict()
199
+
200
+ return _completion_with_retry(**kwargs)
201
+
202
+ def _combine_llm_outputs(self, llm_outputs: List[Optional[dict]]) -> dict:
203
+ overall_token_usage: dict = {}
204
+ for output in llm_outputs:
205
+ if output is None:
206
+ # Happens in streaming
207
+ continue
208
+ token_usage = output['token_usage']
209
+ if token_usage is None:
210
+ continue
211
+
212
+ for k, v in token_usage.items():
213
+ if k in overall_token_usage:
214
+ overall_token_usage[k] += v
215
+ else:
216
+ overall_token_usage[k] = v
217
+ return {
218
+ 'token_usage': overall_token_usage,
219
+ 'model_name': self.model_name
220
+ }
221
+
222
+ def _generate(
223
+ self,
224
+ messages: List[BaseMessage],
225
+ stop: Optional[List[str]] = None,
226
+ run_manager: Optional[CallbackManagerForLLMRun] = None,
227
+ **kwargs: Any,
228
+ ) -> ChatResult:
229
+ message_dicts, params = self._create_message_dicts(messages, stop)
230
+ params = {**params, **kwargs}
231
+ response = self.completion_with_retry(messages=message_dicts, **params)
232
+ return self._create_chat_result(response)
233
+
234
+ async def _agenerate(
235
+ self,
236
+ messages: List[BaseMessage],
237
+ stop: Optional[List[str]] = None,
238
+ run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
239
+ **kwargs: Any,
240
+ ) -> ChatResult:
241
+ return self._generate(messages, stop, run_manager, kwargs)
242
+
243
+ def _create_message_dicts(
244
+ self, messages: List[BaseMessage], stop: Optional[List[str]]
245
+ ) -> Tuple[List[Dict[str, Any]], Dict[str, Any]]:
246
+ params = dict(self._client_params)
247
+ if stop is not None:
248
+ if 'stop' in params:
249
+ raise ValueError(
250
+ '`stop` found in both the input and default params.')
251
+ params['stop'] = stop
252
+
253
+ message_dicts = [_convert_message_to_dict(m) for m in messages]
254
+
255
+ return message_dicts, params
256
+
257
+ def _create_chat_result(self, response: Mapping[str, Any]) -> ChatResult:
258
+ generations = []
259
+ for res in response['choices']:
260
+ message = _convert_dict_to_message(res['message'])
261
+ gen = ChatGeneration(message=message)
262
+ generations.append(gen)
263
+
264
+ llm_output = {
265
+ 'token_usage': response['usage'],
266
+ 'model_name': self.model_name
267
+ }
268
+ return ChatResult(generations=generations, llm_output=llm_output)
269
+
270
+ @property
271
+ def _identifying_params(self) -> Mapping[str, Any]:
272
+ """Get the identifying parameters."""
273
+ return {**{'model_name': self.model_name}, **self._default_params}
274
+
275
+ @property
276
+ def _client_params(self) -> Mapping[str, Any]:
277
+ """Get the parameters used for the client."""
278
+ minimaxai_creds: Dict[str, Any] = {
279
+ 'model': self.model_name,
280
+ }
281
+ return {**minimaxai_creds, **self._default_params}
282
+
283
+ def _get_invocation_params(self,
284
+ stop: Optional[List[str]] = None,
285
+ **kwargs: Any) -> Dict[str, Any]:
286
+ """Get the parameters used to invoke the model FOR THE CALLBACKS."""
287
+ return {
288
+ **super()._get_invocation_params(stop=stop, **kwargs),
289
+ **self._default_params,
290
+ 'model': self.model_name,
291
+ 'function': kwargs.get('functions'),
292
+ }
293
+
294
+ @property
295
+ def _llm_type(self) -> str:
296
+ """Return type of chat model."""
297
+ return 'minimaxai_chat'
298
+
299
+ def _get_encoding_model(self) -> Tuple[str, tiktoken.Encoding]:
300
+ tiktoken_ = _import_tiktoken()
301
+ if self.tiktoken_model_name is not None:
302
+ model = self.tiktoken_model_name
303
+ else:
304
+ model = self.model_name
305
+ # model chatglm-std, chatglm-lite
306
+ # Returns the number of tokens used by a list of messages.
307
+ try:
308
+ encoding = tiktoken_.encoding_for_model(model)
309
+ except KeyError:
310
+ logger.warning(
311
+ 'Warning: model not found. Using cl100k_base encoding.')
312
+ model = 'cl100k_base'
313
+ encoding = tiktoken_.get_encoding(model)
314
+ return model, encoding
315
+
316
+ def get_token_ids(self, text: str) -> List[int]:
317
+ """Get the tokens present in the text with tiktoken package."""
318
+ # tiktoken NOT supported for Python 3.7 or below
319
+ if sys.version_info[1] <= 7:
320
+ return super().get_token_ids(text)
321
+ _, encoding_model = self._get_encoding_model()
322
+ return encoding_model.encode(text)
323
+
324
+ def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
325
+ """Calculate num tokens for chatglm with tiktoken package.
326
+
327
+ todo: read chatglm document
328
+ Official documentation: https://github.com/openai/openai-cookbook/blob/
329
+ main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb"""
330
+ if sys.version_info[1] <= 7:
331
+ return super().get_num_tokens_from_messages(messages)
332
+ model, encoding = self._get_encoding_model()
333
+ if model.startswith('chatglm'):
334
+ # every message follows <im_start>{role/name}\n{content}<im_end>\n
335
+ tokens_per_message = 4
336
+ # if there's a name, the role is omitted
337
+ tokens_per_name = -1
338
+ else:
339
+ raise NotImplementedError(
340
+ f'get_num_tokens_from_messages() is not presently implemented '
341
+ f'for model {model}.'
342
+ 'See https://github.com/openai/openai-python/blob/main/chatml.md for '
343
+ 'information on how messages are converted to tokens.')
344
+ num_tokens = 0
345
+ messages_dict = [_convert_message_to_dict(m) for m in messages]
346
+ for message in messages_dict:
347
+ num_tokens += tokens_per_message
348
+ for key, value in message.items():
349
+ num_tokens += len(encoding.encode(value))
350
+ if key == 'name':
351
+ num_tokens += tokens_per_name
352
+ # every reply is primed with <im_start>assistant
353
+ num_tokens += 3
354
+ return num_tokens