TKimi (Moonshot)
Source: source/classes/tkimi.prg
TKimi is FiveWin's client for the Moonshot Kimi API. It features a unified
Send() method that automatically detects the input type: plain text, local image file,
image URL, or streaming request. This design simplifies integration by eliminating the
need to call different methods for different input types. Kimi models offer strong
multilingual capabilities with context windows from 8K to 128K tokens.
Architecture
flowchart LR
subgraph "FiveWin Application"
A[TKimi
Unified Send()] end subgraph "Input Detection" B[Plain Text] C[Local Image File] D[Image URL] E[Streaming
Callback] end subgraph "Moonshot API" F["api.moonshot.ai/v1/chat/completions"] end A --> B A --> C A --> D A --> E B --> F C --> F D --> F E --> F
Unified Send()] end subgraph "Input Detection" B[Plain Text] C[Local Image File] D[Image URL] E[Streaming
Callback] end subgraph "Moonshot API" F["api.moonshot.ai/v1/chat/completions"] end A --> B A --> C A --> D A --> E B --> F C --> F D --> F E --> F
Key DATA Members
| DATA | Type | Default | Description |
|---|---|---|---|
cKey | Character | "" | Moonshot API key. Reads KIMI_API_KEY env var if empty. |
cModel | Character | "moonshot-v1-8k" | Model name: moonshot-v1-8k, moonshot-v1-32k, or moonshot-v1-128k |
cPrompt | Character | Last prompt sent to the API | |
cResponse | Character | Raw JSON response from the API | |
cUrl | Character | API endpoint URL (auto-set to Moonshot endpoint) | |
hCurl | Handle | libcurl easy handle | |
nError | Numeric | 0 | curl error code (0 = success) |
nHttpCode | Numeric | 0 | HTTP response status code |
nTemperature | Numeric | 0.5 | Sampling temperature (0.0 - 2.0) |
Methods
| Method | Description |
|---|---|
New( cKey, cModel ) | Create a TKimi instance. If cKey is empty, reads KIMI_API_KEY from the environment. |
Send( xInput, uOptional ) | Unified send method. Auto-detects the input type: plain text string, local file path (sends as image), HTTP URL (sends as image URL), or if the second parameter is a codeblock (streaming). |
GetValue() | Extract the text response from the JSON in cResponse. Navigates choices[1].message.content. |
End() | Clean up the libcurl handle. |
Example: Plain Text Prompt
#include "FiveWin.ch"
function Main()
local oKimi := TKimi():New( "your-moonshot-key" )
// Simple text prompt
oKimi:Send( "What is the capital of France?" )
if oKimi:nError == 0
MsgInfo( oKimi:GetValue() )
endif
oKimi:End()
return nil
Example: Image URL Analysis
#include "FiveWin.ch"
function Main()
local oKimi := TKimi():New( "your-moonshot-key" )
// Image URL - auto-detected by the "http" prefix
oKimi:Send( "https://example.com/photo.jpg", ;
"What do you see in this image?" )
if oKimi:nError == 0
MsgInfo( oKimi:GetValue() )
endif
oKimi:End()
return nil
Example: Streaming Response
#include "FiveWin.ch"
function Main()
local oKimi := TKimi():New( "your-moonshot-key" )
local cBuffer := ""
// Pass a codeblock as the second parameter for streaming
oKimi:Send( "Tell me a joke", { |c| cBuffer += c, QOut( c ) } )
if oKimi:nError == 0
MsgInfo( "Complete:" + CRLF + cBuffer )
endif
oKimi:End()
return nil
Notes
- The
Send()method detects input types automatically: an existing file path triggers image upload, a URL starting with"http"triggers image URL mode, a codeblock as second parameter enables streaming, and plain text sends a standard chat completion request. - TKimi requires the
hb_curllibrary. Include#include "hbcurl.ch"for curl constants. - The API key can be read from the
KIMI_API_KEYenvironment variable by passing an empty key toNew(). - Kimi models:
moonshot-v1-8k(8K context),moonshot-v1-32k(32K context), andmoonshot-v1-128k(128K context). - SSL verification is disabled by default for broad compatibility.