fusion_hat.llm モジュール
Large Language Model (LLM) module
This module provides a base class for large language models (LLMs) and preset LLM classes.
サンプル
import a preset LLM class
>>> from fusion_hat.llm import Deepseek as LLM
>>> from fusion_hat.llm import Grok as LLM
>>> from fusion_hat.llm import Doubao as LLM
>>> from fusion_hat.llm import Qwen as LLM
>>> from fusion_hat.llm import OpenAI as LLM
initialize the LLM instance
>>> API_KEY = "your_api_key"
>>> MODEL = "your_model"
>>> llm = LLM(api_key=API_KEY, model=MODEL)
For Ollama, you don't need api_key, but you might need to set ip.
>>> from fusion_hat.llm import Ollama as LLM
>>> llm = LLM(ip="localhost", model="deepseek-r1:1.5b")
You can also import a basic LLM class.
>>> from fusion_hat.llm import LLM as LLM
You will need to set the base url which compatible with OpenAI completion API.
>>> llm = LLM(
base_url="https://api.deepseek.com",
model=MODEL,
api_key=API_KEY,
)
Or set the whole url if it's not ends with "/v1/chat/completions"
>>> llm = LLM(
url="https://api.deepseek.com/v1/chat/completions",
model=MODEL,
api_key=API_KEY,
)
Set instructions
>>> llm.set_instructions("You are a helpful assistant.")
Set welcome message
>>> llm.set_welcome("Hello, I am a helpful assistant. How can I help you?")
Prompt the LLM with input text
>>> input_text = "Hello"
>>> response = llm.prompt(input_text, stream=True)
>>> for next_word in response:
>>> if next_word:
>>> print(next_word, end="", flush=True)
>>> print("")
Prompt with image
>>> input_text = "Hello"
>>> image = "image.jpg"
>>> response = llm.prompt(input_text, image=image, stream=True)
>>> for next_word in response:
>>> if next_word:
>>> print(next_word, end="", flush=True)
>>> print("")
- class fusion_hat.llm.LLM(api_key=None, model=None, url=None, base_url=None, max_messages=20, authorization=Authorization.BEARER, debug=False)[ソース]
ベースクラス:
objectLLM class
- パラメータ:
api_key (str, optional) -- API key, default is None
model (str, optional) -- Model name, default is None
url (str, optional) -- URL, default is None
base_url (str, optional) -- Base URL, default is None
max_messages (int, optional) -- Max messages, default is DEFAULTMAX_MESSAGES
authorization (Authorization, optional) -- Authorization, default is Authorization.BEARER
- DEFAULTMAX_MESSAGES = 20
- set(name, value)[ソース]
Set parameter
- パラメータ:
name (str) -- Parameter name
value (str) -- Parameter value
- add_message(role, content, image_path=None)[ソース]
Add message
- パラメータ:
role (str) -- Role
content (str) -- Content
image_path (str, optional) -- Image path, default is None
- get_base64_from_image(image_path)[ソース]
Get base64 from image
- パラメータ:
image_path (str) -- Image path
- 戻り値:
Base64 string
- 戻り値の型:
str
- get_base_64_url_from_image(image_path)[ソース]
Get base64 url from image
- パラメータ:
image_path (str) -- Image path
- 戻り値:
Base64 url
- 戻り値の型:
str
- chat(stream=False, **kwargs)[ソース]
Chat with LLM
- パラメータ:
stream (bool, optional) -- Stream, default is False
**kwargs -- Additional arguments
- 戻り値:
Response
- 戻り値の型:
requests.Response
- prompt(msg, image_path=None, stream=False, **kwargs)[ソース]
Prompt LLM
- パラメータ:
msg (str or list) -- Message
image_path (str, optional) -- Image path, default is None
stream (bool, optional) -- Stream, default is False
**kwargs -- Additional arguments
- 戻り値:
Response
- 戻り値の型:
requests.Response
- 例外:
ValueError -- Model not set
ValueError -- API key not set
ValueError -- URL not set
ValueError -- Prompt must be a string or a list of messages
- decode_stream_response(line)[ソース]
Decode stream response
- パラメータ:
line (str) -- Line
- 戻り値:
Content
- 戻り値の型:
str
- _stream_response(response)[ソース]
Stream response
- パラメータ:
response (requests.Response) -- Response
- 列挙:
str -- Content
- class fusion_hat.llm.Deepseek(*args, **kwargs)[ソース]
ベースクラス:
LLMDeepseek preset LLM class
- パラメータ:
*args -- Passed to
sunfounder_voice_assistant.llm.llm.LLM**kwargs -- Passed to
sunfounder_voice_assistant.llm.llm.LLM
- class fusion_hat.llm.Grok(*args, **kwargs)[ソース]
ベースクラス:
LLMGrok preset LLM class
- パラメータ:
*args -- Passed to
sunfounder_voice_assistant.llm.llm.LLM**kwargs -- Passed to
sunfounder_voice_assistant.llm.llm.LLM
- class fusion_hat.llm.Doubao(*args, **kwargs)[ソース]
ベースクラス:
LLMDoubao preset LLM class
- パラメータ:
*args -- Passed to
sunfounder_voice_assistant.llm.llm.LLM**kwargs -- Passed to
sunfounder_voice_assistant.llm.llm.LLM
- class fusion_hat.llm.Qwen(*args, **kwargs)[ソース]
ベースクラス:
LLMQwen preset LLM class
- パラメータ:
*args -- Passed to
sunfounder_voice_assistant.llm.llm.LLM**kwargs -- Passed to
sunfounder_voice_assistant.llm.llm.LLM
- class fusion_hat.llm.OpenAI(*args, **kwargs)[ソース]
ベースクラス:
LLMOpenAI preset LLM class
- パラメータ:
*args -- Passed to
sunfounder_voice_assistant.llm.llm.LLM**kwargs -- Passed to
sunfounder_voice_assistant.llm.llm.LLM
- class fusion_hat.llm.Ollama(ip: str = 'localhost', *args, api_key: str = 'ollama', **kwargs)[ソース]
ベースクラス:
LLMOllama preset LLM class
- パラメータ:
ip (str, optional) -- IP address of Ollama server. Defaults to "localhost".
*args -- Passed to
sunfounder_voice_assistant.llm.llm.LLMapi_key (str, optional) -- API key of Ollama server. Defaults to "ollama".
**kwargs -- Passed to
sunfounder_voice_assistant.llm.llm.LLM
- add_message(role: str, content: str, image_path: str = None) None[ソース]
Add message to messages list
- パラメータ:
role (str) -- Role of message, e.g. "user", "assistant"
content (str) -- Content of message
image_path (str, optional) -- Image path, default is None
- 例外:
ValueError -- Role must be 'user' or 'assistant'
- class fusion_hat.llm.Gemini(*args, **kwargs)[ソース]
ベースクラス:
LLMGemini preset LLM class
- パラメータ:
*args -- Passed to
sunfounder_voice_assistant.llm.llm.LLM**kwargs -- Passed to
sunfounder_voice_assistant.llm.llm.LLM