LLM Integration#
The cyber-doctor project integrates with large language models through a factory pattern that routes requests to two distinct client families:
- OpenAI-compatible clients (
OurAPI/LLMclientgeneric) — for all text-based tasks - ZhipuAI SDK clients — for multimodal tasks (image generation, image description, video generation)
The entry point for all client construction is Clientfactory.
Client Architecture#
Clientfactory
├── get_client() → OurAPI (text generation, RAG, KG, search, PPT, Docx)
└── get_special_client() → ZhipuAI clients (image/video tasks)
LLMclientbase defines the abstract interface. It initializes an openai.OpenAI instance using three environment variables :
| Env Variable | Purpose |
|---|---|
LLM_BASE_URL | API endpoint (supports any OpenAI-compatible provider) |
LLM_API_KEY | API authentication key |
MODEL_NAME | Model to invoke (e.g., glm-4-flash) |
LLMclientgeneric provides the concrete implementation with three callable methods:
chat_with_ai(prompt)— single-turn, non-streaming; used for structured generation (PPT, Docx)chat_with_ai_stream(prompt, history)— multi-turn streaming; primary method for interactive chatchat_using_messages(messages)— accepts a pre-built message list; used when callers perform their own prompt construction
All text calls use top_p=0.7, temperature=0.95, max_tokens=1024 .
OurAPI is a thin subclass of LLMclientgeneric with no additional logic — it exists to allow provider-specific overrides in future.
ZhipuAI Multimodal Clients#
Three ZhipuAI client instances are module-level singletons defined in client/zhipuAPI/client.py, each backed by a separate API key env var:
| Client | Env Key | Default Model |
|---|---|---|
Image_generate_client | IMAGE_GENERATE_API | cogview-3-flash |
Image_describe_client | IMAGE_DESCRIBE_API | glm-4v-flash |
Video_generate_client | VIDEO_GENERATE_API | cogvideox-flash |
Clientfactory.get_special_client(client_type) selects the right instance based on userPurposeType .
How Each Modality Is Called#
All multimodal API calls are in qa/function_tool.py:
- Image generation —
client.images.generations(model=..., prompt=...) - Image description —
client.chat.completions.create()with a multimodal message payload (text +image_url) - Video generation — async two-step:
client.videos.generations()returns a job ID; the caller pollsclient.videos.retrieve_videos_result(id=...)untiltask_statusis complete, then readsvideo_result[0].url
Routing by User Intent#
userPurposeType is the central enum mapping user intent to the correct client and handler. Text-centric types (text, RAG, KnowledgeGraph, PPT, Docx, InternetSearch, DeepResearch) all go through Clientfactory().get_client() → OurAPI. Multimodal types (ImageGeneration, ImageDescribe, Video) go through Clientfactory.get_special_client() → the relevant ZhipuAI singleton.
Key Files#
| File | Role |
|---|---|
client/LLMclientbase.py | Abstract base class; OpenAI client init |
client/LLMclientgeneric.py | Concrete text-chat implementation |
client/ourAPI/client.py | Thin provider wrapper (OurAPI) |
client/clientfactory.py | Factory; routes to text or multimodal client |
client/zhipuAPI/client.py | ZhipuAI singleton clients |
qa/purpose_type.py | userPurposeType enum + intent map |
qa/function_tool.py | Actual API call implementations per modality |