converters.go

Overview

The converters.go file provides utility functions to convert responses from the genai API format into the internal model package's format, specifically converting genai.GenerateContentResponse objects into model.LLMResponse objects. This conversion abstracts the differences between the external LLM response representation and the internal application model, enabling seamless integration of generative AI responses into the broader system's workflows and data structures.

Function Details

Genai2LLMResponse

func Genai2LLMResponse(res *genai.GenerateContentResponse) *model.LLMResponse

Purpose

Transforms a pointer to a genai.GenerateContentResponse into a pointer to a model.LLMResponse. This function extracts relevant fields from the external response and populates the internal response structure accordingly, including handling error conditions and usage metadata.

Parameters

Returns

Behavior and Usage

This function ensures that all relevant metadata and error information is preserved and translated into the internal format expected by the rest of the system.

Example Usage

genaiResponse := &genai.GenerateContentResponse{
    // populated from genai API call
}

llmResponse := Genai2LLMResponse(genaiResponse)
// llmResponse can now be used within the internal system's LLM handling components

Implementation Details

Interaction with Other System Components

Visual Diagram

flowchart TD
A[genai.GenerateContentResponse] -->|Genai2LLMResponse| B[model.LLMResponse]
B --> C{Content Present?}
C -- Yes --> D[Populate Content, Metadata, Logprobs]
C -- No --> E{PromptFeedback Present?}
E -- Yes --> F[Populate Error from PromptFeedback]
E -- No --> G[Return Unknown Error Response]

This flowchart illustrates the decision-making process inside the Genai2LLMResponse function, showing how different branches of the input response lead to different forms of the output LLMResponse.