LLM 用戶端¶
Gemini 和 Azure OpenAI 需要安裝 llm extra。
Warning
generate_response 會呼叫遠端模型:輸出不固定,每次呼叫都會用掉 API 額度。
cjtrade.pkgs.llm._llm_base.LLMClientBase(model_name, api_key=None)
¶
Base class for LLM clients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Name of the model to use. |
required |
api_key
|
str
|
API key for the provider. |
None
|
Source code in src/cjtrade/pkgs/llm/_llm_base.py
11 12 13 14 | |
generate_response(prompt)
¶
Return a reply to prompt; subclasses must override this.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The full prompt. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A fixed message saying the method is not implemented. |
Warnings
Placeholder: the base class does not call any model.
Source code in src/cjtrade/pkgs/llm/_llm_base.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
cjtrade.pkgs.llm.gemini
¶
GeminiClient(model_name='gemini-3-flash-preview', api_key=None)
¶
Bases: LLMClientBase
LLM client for Google Gemini (google-genai).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Gemini model name. |
"gemini-3-flash-preview"
|
api_key
|
str
|
Gemini API key. |
None
|
Source code in src/cjtrade/pkgs/llm/gemini.py
20 21 22 | |
generate_response(prompt)
¶
Send prompt to Gemini and return its text reply.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The full prompt. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The model's reply. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If the 'llm' extra ( |
Warnings
Calls a remote API over the network: output is non-deterministic and each call uses paid quota.
Source code in src/cjtrade/pkgs/llm/gemini.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
cjtrade.pkgs.llm.azure_openai
¶
AzureOpenAIClient(model_name='gpt-4o', api_key=None, deployment='gpt-4o', endpoint='')
¶
Bases: LLMClientBase
LLM client for Azure OpenAI chat completions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Name of the model. |
"gpt-4o"
|
api_key
|
str
|
Azure OpenAI API key. |
None
|
deployment
|
str
|
Azure deployment name sent as the request's model. |
"gpt-4o"
|
endpoint
|
str
|
Azure OpenAI endpoint URL. |
""
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If the 'llm' extra ( |
Source code in src/cjtrade/pkgs/llm/azure_openai.py
28 29 30 31 32 33 34 35 36 37 | |
generate_response(prompt)
¶
Send prompt to the Azure deployment and return its text reply.
A fixed system message ("You're a quant trading assistant.") is prepended.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The user prompt. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The model's reply. |
Warnings
Calls a remote API over the network: output is non-deterministic (temperature 1.0) and each call uses paid quota.
Source code in src/cjtrade/pkgs/llm/azure_openai.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
cjtrade.pkgs.llm.chatpdf
¶
ChatPDFClient(model_name='chatpdf', api_key=None, pdf_src=None)
¶
Bases: LLMClientBase
LLM client that asks questions about a PDF via the ChatPDF API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Name of the model. |
"chatpdf"
|
api_key
|
str
|
ChatPDF API key. |
None
|
pdf_src
|
str
|
ChatPDF source ID of the uploaded PDF. |
None
|
Source code in src/cjtrade/pkgs/llm/chatpdf.py
19 20 21 22 | |
generate_response(prompt)
¶
Send prompt about the PDF to ChatPDF and return its reply.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The user message. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The |
Raises:
| Type | Description |
|---|---|
Exception
|
If the API returns a non-200 status; the message is the response body. |
Warnings
Calls a remote API over the network: output is non-deterministic and each call uses API quota.
Source code in src/cjtrade/pkgs/llm/chatpdf.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
cjtrade.pkgs.llm.mock_llm
¶
MockLLMClient(model_name='', api_key=None)
¶
Bases: LLMClientBase
Offline LLM client for tests that echoes the prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Name of the model (unused). |
""
|
api_key
|
str
|
API key (unused). |
None
|
Source code in src/cjtrade/pkgs/llm/mock_llm.py
15 16 17 | |
generate_response(prompt)
¶
Wait one second and return "Hello from MockLLM: <prompt>".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt to echo. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The canned reply. |
Source code in src/cjtrade/pkgs/llm/mock_llm.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
cjtrade.pkgs.llm.llm_pool
¶
LLMPool(llms)
¶
Bases: LLMClientBase
A list of LLM clients that fails over to the next one on error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llms
|
list of LLMClientBase
|
Clients to try, in order. |
required |
Source code in src/cjtrade/pkgs/llm/llm_pool.py
15 16 17 | |
get_current_llm()
¶
Return the client currently in use.
Raises:
| Type | Description |
|---|---|
Exception
|
If the pool is empty. |
Source code in src/cjtrade/pkgs/llm/llm_pool.py
19 20 21 22 23 24 25 26 27 28 29 | |
get_next_llm()
¶
Return the client after the current one, without switching to it.
Raises:
| Type | Description |
|---|---|
Exception
|
If the pool is empty. |
Source code in src/cjtrade/pkgs/llm/llm_pool.py
31 32 33 34 35 36 37 38 39 40 41 42 | |
generate_response(prompt)
¶
Return a reply from the current client, failing over on errors.
On an exception the pool logs the error, moves to the next client (wrapping around) and retries until some client returns a reply.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The full prompt. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The first successful reply. |
Warnings
The member clients may call remote APIs (network, usage costs). If every client keeps failing, this loops forever.
Source code in src/cjtrade/pkgs/llm/llm_pool.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |