The name of the model to use
Cohere async client.
The name of the model to use
Holds any model parameters valid for create call not explicitly specified.
Timeout in seconds for the LocalAI request.
Number of chat completions to generate for each prompt.
The model name to pass to tiktoken when using this class.
Optional httpx.Client.
Moonshot chat model integration.
Setup:
Install openai and set environment variables MOONSHOT_API_KEY.
.. code-block:: bash
pip install openai
export MOONSHOT_API_KEY="your-api-key"
Key init args — completion params: model: str Name of Moonshot model to use. temperature: float Sampling temperature. max_tokens: Optional[int] Max number of tokens to generate.
Key init args — client params: api_key: Optional[str] Moonshot API KEY. If not passed in will be read from env var MOONSHOT_API_KEY. api_base: Optional[str] Base URL for API requests.
See full list of supported init args and their descriptions in the params section.
Instantiate:
.. code-block:: python
from langchain_community.chat_models import MoonshotChat
chat = MoonshotChat( temperature=0.5, api_key="your-api-key", model="moonshot-v1-8k", # api_base="...", # other params... )
Invoke:
.. code-block:: python
messages = [
("system", "你是一名专业的翻译家,可以将用户的中文翻译为英文。"),
("human", "我喜欢编程。"),
]
chat.invoke(messages)
.. code-block:: python
AIMessage(
content='I like programming.',
additional_kwargs={},
response_metadata={
'token_usage': {
'completion_tokens': 5,
'prompt_tokens': 27,
'total_tokens': 32
},
'model_name': 'moonshot-v1-8k',
'system_fingerprint': None,
'finish_reason': 'stop',
'logprobs': None
},
id='run-71c03f4e-6628-41d5-beb6-d2559ae68266-0'
)
Stream:
.. code-block:: python
for chunk in chat.stream(messages):
print(chunk)
.. code-block:: python
content='' additional_kwargs={} response_metadata={} id='run-80d77096-8b83-4c39-a84d-71d9c746da92'
content='I' additional_kwargs={} response_metadata={} id='run-80d77096-8b83-4c39-a84d-71d9c746da92'
content=' like' additional_kwargs={} response_metadata={} id='run-80d77096-8b83-4c39-a84d-71d9c746da92'
content=' programming' additional_kwargs={} response_metadata={} id='run-80d77096-8b83-4c39-a84d-71d9c746da92'
content='.' additional_kwargs={} response_metadata={} id='run-80d77096-8b83-4c39-a84d-71d9c746da92'
content='' additional_kwargs={} response_metadata={'finish_reason': 'stop'} id='run-80d77096-8b83-4c39-a84d-71d9c746da92'
.. code-block:: python
stream = chat.stream(messages)
full = next(stream)
for chunk in stream:
full += chunk
full
.. code-block:: python
AIMessageChunk(
content='I like programming.',
additional_kwargs={},
response_metadata={'finish_reason': 'stop'},
id='run-10c80976-7aa5-4ff7-ba3e-1251665557ef'
)
Async:
.. code-block:: python
await chat.ainvoke(messages)
# stream:
# async for chunk in chat.astream(messages):
# print(chunk)
# batch:
# await chat.abatch([messages])
.. code-block:: python
[AIMessage(content='I like programming.', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 5, 'prompt_tokens': 27, 'total_tokens': 32}, 'model_name': 'moonshot-v1-8k', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-2938b005-9204-4b9f-b273-1c3272fce9e5-0')]
Response metadata .. code-block:: python
ai_msg = chat.invoke(messages)
ai_msg.response_metadata
.. code-block:: python
{
'token_usage': {
'completion_tokens': 5,
'prompt_tokens': 27,
'total_tokens': 32
},
'model_name': 'moonshot-v1-8k',
'system_fingerprint': None,
'finish_reason': 'stop',
'logprobs': None
}
Get the tokens present in the text with tiktoken package.