xrag.llms package#
Submodules#
xrag.llms.chatglm4 module#
- class xrag.llms.chatglm4.ChatGLM(model='glm-4', reuse_client=True, api_key=None, **kwargs)[source]#
Bases:
CustomLLM
-
api_key:
str
#
- chat(messages, **kwargs)[source]#
Chat endpoint for LLM.
- Parameters:
messages (Sequence[ChatMessage]) – Sequence of chat messages.
kwargs (Any) – Additional keyword arguments to pass to the LLM.
- Returns:
Chat response from the LLM.
- Return type:
ChatResponse
Examples
```python from llama_index.core.llms import ChatMessage
response = llm.chat([ChatMessage(role=”user”, content=”Hello”)]) print(response.content) ```
- classmethod class_name()[source]#
Get the class name, used as a unique ID in serialization.
This provides a key that makes serialization robust against actual class name changes.
- Return type:
str
- complete(*args, **kwargs)[source]#
Completion endpoint for LLM.
If the LLM is a chat model, the prompt is transformed into a single user message.
- Parameters:
prompt (str) – Prompt to send to the LLM.
formatted (bool, optional) – Whether the prompt is already formatted for the LLM, by default False.
kwargs (Any) – Additional keyword arguments to pass to the LLM.
- Returns:
Completion response from the LLM.
- Return type:
CompletionResponse
Examples
`python response = llm.complete("your prompt") print(response.text) `
-
context_window:
int
#
- property metadata: LLMMetadata#
Get LLM metadata.
-
model:
str
#
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}#
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'api_key': FieldInfo(annotation=str, required=False, default=None, description='The ChatGLM API key.'), 'callback_manager': FieldInfo(annotation=CallbackManager, required=False, default_factory=<lambda>, exclude=True), 'completion_to_prompt': FieldInfo(annotation=Union[CompletionToPromptType, NoneType], required=False, default=None, description='Function to convert a completion to an LLM prompt.', exclude=True, metadata=[WithJsonSchema(json_schema={'type': 'string'}, mode=None)]), 'context_window': FieldInfo(annotation=int, required=False, default=3900, description='The maximum number of context tokens for the model.', metadata=[Gt(gt=0)]), 'messages_to_prompt': FieldInfo(annotation=Union[MessagesToPromptType, NoneType], required=False, default=None, description='Function to convert a list of messages to an LLM prompt.', exclude=True, metadata=[WithJsonSchema(json_schema={'type': 'string'}, mode=None)]), 'model': FieldInfo(annotation=str, required=False, default='glm-4', description='The ChatGlM model to use. glm-4 or glm-3-turbo'), 'num_output': FieldInfo(annotation=int, required=False, default=256), 'output_parser': FieldInfo(annotation=Union[BaseOutputParser, NoneType], required=False, default=None, description='Output parser to parse, validate, and correct errors programmatically.', exclude=True), 'pydantic_program_mode': FieldInfo(annotation=PydanticProgramMode, required=False, default=<PydanticProgramMode.DEFAULT: 'default'>), 'query_wrapper_prompt': FieldInfo(annotation=Union[BasePromptTemplate, NoneType], required=False, default=None, description='Query wrapper prompt for LLM calls.', exclude=True), 'reuse_client': FieldInfo(annotation=bool, required=False, default=True, description='Reuse the client between requests. When doing anything with large volumes of async API calls, setting this to false can improve stability.'), 'system_prompt': FieldInfo(annotation=Union[str, NoneType], required=False, default=None, description='System prompt for LLM calls.')}#
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- model_post_init(context, /)#
This function is meant to behave like a BaseModel method to initialise private attributes.
It takes context as an argument since that’s what pydantic-core passes when calling it.
- Parameters:
self (
BaseModel
) – The BaseModel instance.context (
Any
) – The context.
- Return type:
None
-
num_output:
int
#
-
reuse_client:
bool
#
- stream_chat(messages, **kwargs)[source]#
Streaming chat endpoint for LLM.
- Parameters:
messages (Sequence[ChatMessage]) – Sequence of chat messages.
kwargs (Any) – Additional keyword arguments to pass to the LLM.
- Yields:
ChatResponse – A generator of ChatResponse objects, each containing a new token of the response.
- Return type:
Generator
[CompletionResponse
,None
,None
]
Examples
```python from llama_index.core.llms import ChatMessage
gen = llm.stream_chat([ChatMessage(role=”user”, content=”Hello”)]) for response in gen:
print(response.delta, end=””, flush=True)
- stream_complete(*args, **kwargs)[source]#
Streaming completion endpoint for LLM.
If the LLM is a chat model, the prompt is transformed into a single user message.
- Parameters:
prompt (str) – Prompt to send to the LLM.
formatted (bool, optional) – Whether the prompt is already formatted for the LLM, by default False.
kwargs (Any) – Additional keyword arguments to pass to the LLM.
- Yields:
CompletionResponse – A generator of CompletionResponse objects, each containing a new token of the response.
- Return type:
Generator
[CompletionResponse
,None
,None
]
Examples
```python gen = llm.stream_complete(“your prompt”) for response in gen:
print(response.text, end=””, flush=True)
-
api_key: