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

Key DATA Members

DATATypeDefaultDescription
cKeyCharacter""Moonshot API key. Reads KIMI_API_KEY env var if empty.
cModelCharacter"moonshot-v1-8k"Model name: moonshot-v1-8k, moonshot-v1-32k, or moonshot-v1-128k
cPromptCharacterLast prompt sent to the API
cResponseCharacterRaw JSON response from the API
cUrlCharacterAPI endpoint URL (auto-set to Moonshot endpoint)
hCurlHandlelibcurl easy handle
nErrorNumeric0curl error code (0 = success)
nHttpCodeNumeric0HTTP response status code
nTemperatureNumeric0.5Sampling temperature (0.0 - 2.0)

Methods

MethodDescription
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

See Also