LLM Integration
Relevant source files
The following files were used as context for generating this wiki page:The LLM (Large Language Model) integration is a core component of the aidoit4u mobile application, providing the AI-powered features for document analysis, generation, and chat. The system is designed to communicate with a backend LLM service, specifically the Manus Forge API, which is compatible with the OpenAI chat completions endpoint.
The primary module for this integration is server/_core/llm.ts, which acts as a gateway to the LLM. It abstracts the API communication, handles request and response normalization, and defines the data structures for interacting with the model. This central service is intended to be used by various backend endpoints that power the application's intelligent features.
Sources: server/_core/llm.ts, design.md, todo.md
Architecture Overview
The LLM integration follows a client-server architecture where the mobile app communicates with its own backend server, which in turn makes requests to the external Manus Forge LLM API. This insulates the frontend from the complexities of direct LLM interaction and keeps API keys secure on the server.
This diagram illustrates the high-level data flow for an AI-powered feature like document analysis.
This flow is conceptualized from the frontend implementation in app/(tabs)/analyze.tsx and the backend capabilities in server/_core/llm.ts. The analyze.tsx screen currently uses a mock setTimeout to simulate this flow.
Sources: app/(tabs)/analyze.tsx:61-80, server/_core/llm.ts:285-321
Core LLM Service
The invokeLLM function is the central piece of the LLM integration, responsible for all communication with the chat completions API.
Sources: server/_core/llm.ts
invokeLLM Function
This asynchronous function constructs and sends a request to the Manus Forge API. It handles API key authentication, payload normalization, and error handling.
The internal logic of the function is as follows:
Sources: server/_core/llm.ts:285-321
Configuration
The service relies on environment variables for its configuration.
| Variable | Description | File |
|---|---|---|
ENV.forgeApiUrl | The base URL for the Manus Forge API. Defaults to https://forge.manus.im if not set. | server/_core/llm.ts:221-224 |
ENV.forgeApiKey | The API key for authenticating with the Forge API. The application will throw an error if this is not set. | server/_core/llm.ts:226-230 |
The API endpoint is resolved to /v1/chat/completions.
Sources: server/_core/llm.ts:221-230
Parameters and Return Types
The invokeLLM function uses specific data structures for its parameters (InvokeParams) and return value (InvokeResult).
InvokeParams
| Parameter | Type | Description |
|---|---|---|
messages | Message[] | An array of message objects representing the conversation history. |
tools | Tool[] | (Optional) A list of functions the model may call. |
toolChoice | ToolChoice | (Optional) Controls which function is called by the model. |
maxTokens | number | (Optional) Maximum number of tokens to generate. Hardcoded to 32768. |
outputSchema | OutputSchema | (Optional) Defines a JSON schema for the model's output. |
responseFormat | ResponseFormat | (Optional) Specifies the output format, e.g., json_object. |
Sources: server/_core/llm.ts:68-81
InvokeResult
This is the raw response from the chat completions API.
| Field | Type | Description |
|---|---|---|
id | string | A unique identifier for the chat completion. |
created | number | The Unix timestamp of when the completion was created. |
model | string | The model used for the completion. |
choices | Array | A list of chat completion choices. Usually one. |
usage | object | (Optional) Token usage statistics for the completion. |
Sources: server/_core/llm.ts:95-112
Data Structures
The interaction with the LLM is defined by a set of TypeScript types that model the API contract.
Sources: server/_core/llm.ts:3-112
Message Structure
A Message object is the fundamental unit of conversation. The relationship between the core message types is shown below.
This diagram shows that a Message has a role and content, where content can be an array of different types.
Sources: server/_core/llm.ts:3-30
The Role can be one of "system", "user", "assistant", "tool", or "function".
Sources: server/_core/llm.ts:3
Request and Response Normalization
Several helper functions are used to process the InvokeParams before sending them to the API.
normalizeMessage(message): Converts thecontentproperty of a message into the format expected by the API. If content is a simple string, it's wrapped in aTextContentobject. If it's an array, each part is normalized. Fortoolorfunctionroles, content is stringified.normalizeToolChoice(toolChoice, tools): Resolves thetoolChoiceparameter. It can handle keywords like"required"and automatically select the tool if only one is provided.normalizeResponseFormat(...): ConsolidatesresponseFormatandoutputSchemaparameters into a singleresponse_formatobject for the API payload, ensuring that if a JSON schema is requested, it is correctly formatted.
Sources: server/_core/llm.ts:139-219
Related AI Services
The Manus Forge API is also used for other AI-related tasks, such as voice transcription, demonstrating a broader integration with the AI platform.
Voice Transcription
The transcribeAudio function in server/_core/voiceTranscription.ts uses the Forge API to convert speech to text.
- Endpoint: It calls the
/v1/audio/transcriptionsendpoint. - Authentication: It uses the same
ENV.forgeApiKeyas the LLM service. - Process: It downloads an audio file from a given URL (which could be a URL from
server/storage.ts), creates aFormDataobject, and sends it to the Whisper-1 model via the Forge API.
This service is designed to be called from the frontend, for example, after a user records their voice and the audio is uploaded to storage.
Sources: server/_core/voiceTranscription.ts:1-193, server/storage.ts
Frontend Integration and Use Cases
The application is designed to leverage the LLM for several key features, as outlined in the design and planning documents.
| Feature | Description | Status | Source Files |
|---|---|---|---|
| Document Analysis | Upload a legal document (PDF, Word, or text) for AI analysis of key clauses, risks, and recommendations. | Partially Implemented (UI with mock data) | app/(tabs)/analyze.tsx, design.md |
| Document Generation | Create professional legal documents (contracts, lawsuits, etc.) based on user-provided details. | UI Scaffolding Present | design.md, todo.md |
| AI Chat | Ask questions in natural language about uploaded documents. | UI Scaffolding Present | design.md, todo.md |
The AnalyzeScreen (app/(tabs)/analyze.tsx) provides a UI for uploading a document or pasting text. The handleAnalyze function currently uses a setTimeout to simulate an API call and displays mock results. In a production scenario, this function would make an API call to a backend endpoint that utilizes the invokeLLM service.
Sources: app/(tabs)/analyze.tsx:61-80, design.md, todo.md
Conclusion
The LLM integration, centered around the invokeLLM function, is a foundational system for the application's core value proposition. It provides a robust and standardized way to interact with the powerful Manus Forge API for various AI tasks, including chat completions and voice transcription. While the frontend features are still in development with mock data, the backend infrastructure in server/_core/llm.ts establishes a clear pattern for building out these intelligent capabilities.