### Frontend Setup for LLM Gateway Source: https://github.com/mylxsw/llm-gateway/blob/main/README.md Install frontend dependencies and run development or production builds. ```bash cd frontend # Install dependencies npm install # Development npm run dev # Production build npm run build && npm run start ``` -------------------------------- ### Backend Setup for LLM Gateway Source: https://github.com/mylxsw/llm-gateway/blob/main/README.md Install backend dependencies and initialize the database. Recommended to use 'uv sync' for dependency management. ```bash cd backend # Install dependencies (choose one) uv sync # Recommended: using uv pip install -r requirements.txt # Or using pip # Initialize database alembic upgrade head # Start server uvicorn app.main:app --host 0.0.0.0 --port 8000 ``` -------------------------------- ### Manual Development Setup Source: https://context7.com/mylxsw/llm-gateway/llms.txt Steps for setting up Squirrel manually for development, including backend and frontend installations and startup commands. ```bash # Backend cd backend uv sync alembic upgrade head uvicorn app.main:app --host 0.0.0.0 --port 8000 # Frontend (separate terminal) cd frontend npm install npm run dev # http://localhost:3000 ``` -------------------------------- ### Clone Repository and Start Services with Docker Compose Source: https://github.com/mylxsw/llm-gateway/blob/main/README.md Use this command to quickly set up the LLM Gateway using Docker Compose for a production-like environment. Ensure Docker and Docker Compose are installed. ```bash git clone https://github.com/mylxsw/llm-gateway.git cd llm-gateway docker compose -f docker-compose.prod.yml up -d ``` -------------------------------- ### Install API Protocol Converter Source: https://github.com/mylxsw/llm-gateway/blob/main/llm_api_converter/README.md Install the library using pip or from source. ```bash pip install api-protocol-converter ``` ```bash git clone https://github.com/example/api-protocol-converter.git cd api-protocol-converter pip install -e . ``` -------------------------------- ### Generate Product Mockup with Python SDK Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/image-generation.md Use the Python SDK to generate product mockups. This example creates a realistic product photograph and saves it as a PNG file. Ensure you have the necessary libraries installed. ```python from google import genai from google.genai import types client = genai.Client() response = client.models.generate_content( model="gemini-2.5-flash-image", contents="A high-resolution, studio-lit product photograph of a minimalist ceramic coffee mug in matte black, presented on a polished concrete surface. The lighting is a three-point softbox setup designed to create soft, diffused highlights and eliminate harsh shadows. The camera angle is a slightly elevated 45-degree shot to showcase its clean lines. Ultra-realistic, with sharp focus on the steam rising from the coffee. Square image.", ) for part in response.parts: if part.text is not None: print(part.text) elif part.inline_data is not None: image = part.as_image() image.save("product_mockup.png") ``` -------------------------------- ### Install Frontend Dependencies Source: https://github.com/mylxsw/llm-gateway/blob/main/GEMINI.md Installs frontend project dependencies using pnpm. Navigate to the 'frontend' directory first. ```bash cd frontend pnpm install ``` -------------------------------- ### Install Backend Dependencies Source: https://github.com/mylxsw/llm-gateway/blob/main/GEMINI.md Installs backend project dependencies using uv or pip. Ensure you are in the 'backend' directory. ```bash cd backend # using uv uv sync # OR using pip pip install -r requirements.txt ``` -------------------------------- ### Go SDK Example Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/image-generation.md Example of using the Go SDK to generate an image with specific aspect ratio and resolution, and to update an existing infographic to Spanish. ```APIDOC ## Go SDK Image Generation ### Description This snippet demonstrates how to configure image generation parameters like aspect ratio and resolution, and how to send a message to the chat model for image generation or modification. ### Method `chat.SendMessage` ### Parameters - `ctx`: Context for the request. - `message`: The text prompt for image generation or modification. - `model.GenerationConfig.ImageConfig`: Configuration for image generation. - `AspectRatio` (string): Desired aspect ratio (e.g., "16:9"). - `ImageSize` (string): Desired resolution (e.g., "2K"). ### Request Example ```go message := "Update this infographic to be in Spanish. Do not change any other elements of the image." aspectRatio := "16:9" resolution := "2K" model.GenerationConfig.ImageConfig = &pb.ImageConfig{ AspectRatio: aspectRatio, ImageSize: resolution, } resp, err := chat.SendMessage(ctx, genai.Text(message)) if err != nil { log.Fatal(err) } for _, part := range resp.Candidates[0].Content.Parts { if txt, ok := part.(genai.Text); ok { fmt.Printf("%s", string(txt)) } else if img, ok := part.(genai.ImageData); ok { err := os.WriteFile("photosynthesis_spanish.png", img.Data, 0644) if err != nil { log.Fatal(err) } } } ``` ``` -------------------------------- ### Start Frontend Development Server Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/changes/QUICKSTART_STREAM_FEATURE.md Run this command in a separate terminal to start the frontend development server. ```bash cd frontend npm run dev ``` -------------------------------- ### Install Backend Dependencies Source: https://github.com/mylxsw/llm-gateway/blob/main/backend/migrations/README.md Installs Python dependencies for the backend, including migration scripts. Run this command from the backend directory. ```bash cd backend uv sync ``` -------------------------------- ### Create and Get Cache Entry - Node.js Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/caching-spec.md This Node.js example demonstrates uploading a file, creating a cache entry with the file and system instructions, and then retrieving the cache entry using its name. ```javascript // Make sure to include the following import: // import {GoogleGenAI} from '@google/genai'; const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); const filePath = path.join(media, "a11.txt"); const document = await ai.files.upload({ file: filePath, config: { mimeType: "text/plain" }, }); console.log("Uploaded file name:", document.name); const modelName = "gemini-1.5-flash-001"; const contents = [ createUserContent(createPartFromUri(document.uri, document.mimeType)), ]; const cache = await ai.caches.create({ model: modelName, config: { contents: contents, systemInstruction: "You are an expert analyzing transcripts.", }, }); const retrievedCache = await ai.caches.get({ name: cache.name }); console.log("Retrieved Cache:", retrievedCache); ``` -------------------------------- ### Generate Content - Go Example Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/generate-content-spec.md Example of how to generate content using the Gemini API in Go. ```Go ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GEMINI_API_KEY"), Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } contents := []*genai.Content{ genai.NewContentFromText("Write a story about a magic backpack.", genai.RoleUser), } response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil) if err != nil { log.Fatal(err) } printResponse(response); ``` -------------------------------- ### Start Frontend Development Server Source: https://github.com/mylxsw/llm-gateway/blob/main/GEMINI.md Starts the Next.js development server. Access the frontend application via http://localhost:3000. ```bash pnpm dev ``` -------------------------------- ### Environment Configuration Example Source: https://context7.com/mylxsw/llm-gateway/llms.txt Example `.env` file demonstrating various configuration options for Squirrel, including database settings, encryption keys, API key generation, and rate limiting. ```env # .env (place in backend/) APP_NAME=LLM Gateway DEBUG=false # Database — SQLite (default) or PostgreSQL DATABASE_TYPE=postgresql DATABASE_URL=postgresql+asyncpg://user:password@localhost:5432/llm_gateway # Encryption (required for production; changing this key makes stored keys unreadable) ENCRYPTION_KEY= # Retry RETRY_MAX_ATTEMPTS=3 RETRY_DELAY_MS=1000 # Upstream timeout (30 min default, supports long streams) HTTP_TIMEOUT=1800 # API Key generation API_KEY_PREFIX=lgw- API_KEY_LENGTH=32 # Optional admin authentication ADMIN_USERNAME=admin ADMIN_PASSWORD=changeme ADMIN_TOKEN_TTL_SECONDS=86400 # Log retention LOG_RETENTION_DAYS=90 LOG_DETAIL_RETENTION_DAYS=7 LOG_CLEANUP_INTERVAL_HOURS=24 # KV Store backend: "database" (default) or "redis" KV_STORE_TYPE=database REDIS_URL=redis://localhost:6379/0 # Rate limiting RATE_LIMIT_ENABLED=true RATE_LIMIT_DEFAULT=100/minute RATE_LIMIT_ADMIN=20/minute RATE_LIMIT_PROXY=200/minute # CORS (comma-separated origins; empty = no CORS in production) ALLOWED_ORIGINS=https://my-app.example.com,http://localhost:3000 # Docker Compose host port LLM_GATEWAY_PORT=8000 ``` -------------------------------- ### Go Image Generation and Editing Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/image-generation.md Illustrates image generation and editing using the Google GenAI Go SDK. This example shows how to initialize the client, start a chat session configured for text and image responses, send a message, and process the results, including saving generated images. ```APIDOC ## Go Image Generation and Editing ### Description Demonstrates using the Google GenAI Go SDK for conversational image generation and editing. It covers client initialization, chat session setup with specified response modalities, sending messages, and handling both text and image outputs. ### Method `client.GenerativeModel` and `model.StartChat` and `chat.SendMessage` ### Parameters - **model name** (string) - Required - The name of the generative model (e.g., `gemini-3-pro-image-preview`). - **GenerationConfig** (object) - Required - Configuration for generation. - **ResponseModalities** (array) - Required - Specifies the expected response types (e.g., `genai.Text`, `genai.Image`). - **message** (string) - Required - The prompt for the AI. ### Response Handling Processes the response parts, printing text content and writing image data to a file. ### Code Example ```go package main import ( "context" "fmt" "log" "os" "google.golang.org/genai" pb "google.golang.org/genai/internal/proto" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, nil) if err != nil { log.Fatal(err) } defer client.Close() model := client.GenerativeModel("gemini-3-pro-image-preview") model.GenerationConfig = &pb.GenerationConfig{ ResponseModalities: []pb.ResponseModality{genai.Text, genai.Image}, } chat := model.StartChat() message := "Create a vibrant infographic that explains photosynthesis as if it were a recipe for a plant's favorite food. Show the \"ingredients\" (sunlight, water, CO2) and the \"finished dish\" (sugar/energy). The style should be like a page from a colorful kids' cookbook, suitable for a 4th grader." resp, err := chat.SendMessage(ctx, genai.Text(message)) if err != nil { log.Fatal(err) } for _, part := range resp.Candidates[0].Content.Parts { if txt, ok := part.(genai.Text); ok { fmt.Printf("%s", string(txt)) } else if img, ok := part.(genai.ImageData); ok { err := os.WriteFile("photosynthesis.png", img.Data, 0644) if err != nil { log.Fatal(err) } } } } ``` ``` -------------------------------- ### Start Backend Development Server Source: https://github.com/mylxsw/llm-gateway/blob/main/GEMINI.md Starts the FastAPI development server with hot-reloading enabled. Access the API via http://127.0.0.1:8000. ```bash uvicorn app.main:app --reload ``` -------------------------------- ### Java SDK Example Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/image-generation.md Example of using the Java SDK to configure image generation settings and send a message for image creation or editing. ```APIDOC ## Java SDK Image Generation ### Description This snippet shows how to set up image generation configurations, including aspect ratio and size, and how to use the `sendMessage` method for image-related tasks. ### Method `chat.sendMessage` ### Parameters - `message` (String): The prompt for image generation or modification. - `config` (GenerateContentConfig): Configuration object for content generation. - `responseModalities` (String[]): Specifies the expected output modalities (e.g., "TEXT", "IMAGE"). - `imageConfig` (ImageConfig): Configuration for image generation. - `aspectRatio` (String): Desired aspect ratio (e.g., "16:9"). - `imageSize` (String): Desired resolution (e.g., "2K"). ### Request Example ```java String aspectRatio = "16:9"; String resolution = "2K"; config = GenerateContentConfig.builder() .responseModalities("TEXT", "IMAGE") .imageConfig(ImageConfig.builder() .aspectRatio(aspectRatio) .imageSize(resolution) .build()) .build(); response = chat.sendMessage( "Update this infographic to be in Spanish. " + "Do not change any other elements of the image.", config); for (Part part : response.parts()) { if (part.text().isPresent()) { System.out.println(part.text().get()); } else if (part.inlineData().isPresent()) { var blob = part.inlineData().get(); if (blob.data().isPresent()) { Files.write(Paths.get("photosynthesis_spanish.png"), blob.data().get()); } } } ``` ``` -------------------------------- ### Run Development Server Source: https://github.com/mylxsw/llm-gateway/blob/main/frontend/README.md Use one of these commands to start the development server. Open http://localhost:3000 in your browser to see the result. ```bash npm run dev # or yarn dev # or pnpm dev # or bun dev ``` -------------------------------- ### Get API Key List Response Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/api.md Example response when retrieving a list of API keys. Note that the key value is sanitized for security. ```json { "items": [ { "id": 1, "key_name": "Production Key", "key_value": "lgw-***...***", // Sanitized display "is_active": true, "created_at": "2024-01-01T00:00:00Z", "last_used_at": "2024-01-10T12:00:00Z" } ], "total": 3, "page": 1, "page_size": 20 } ``` -------------------------------- ### Go Chat Example Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/generate-content-spec.md Sets up a Go client for the Gemini API, creates a chat session with initial history, sends two messages, and prints the responses. Requires GEMINI_API_KEY to be set. ```go ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GEMINI_API_KEY"), Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } // Pass initial history using the History field. history := []*genai.Content{ genai.NewContentFromText("Hello", genai.RoleUser), genai.NewContentFromText("Great to meet you. What would you like to know?", genai.RoleModel), } chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", nil, history) if err != nil { log.Fatal(err) } firstResp, err := chat.SendMessage(ctx, genai.Part{Text: "I have 2 dogs in my house."}) if err != nil { log.Fatal(err) } fmt.Println(firstResp.Text()) secondResp, err := chat.SendMessage(ctx, genai.Part{Text: "How many paws are in my house?"}) if err != nil { log.Fatal(err) } fmt.Println(secondResp.Text()) ``` -------------------------------- ### Get All Model-Provider Mappings Response Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/api.md Example response structure when retrieving all model-provider mappings. It includes details like provider ID, target model, and active status. ```json { "items": [ { "id": 1, "requested_model": "gpt-4", "provider_id": 1, "provider_name": "OpenAI Official", "target_model_name": "gpt-4-0613", "provider_rules": null, "priority": 1, "weight": 1, "is_active": true, "created_at": "2024-01-01T00:00:00Z", "updated_at": "2024-01-01T00:00:00Z" } ], "total": 10 } ``` -------------------------------- ### Get Specific Model Information (Python) Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/models-spec.md Use this snippet to retrieve detailed information about a specific model, such as its version and supported features. Ensure the `google.generativeai` library is installed. ```python from google import genai client = genai.Client() model_info = client.models.get(model="gemini-2.0-flash") print(model_info) ``` -------------------------------- ### Get Log Statistics Source: https://context7.com/mylxsw/llm-gateway/llms.txt Retrieves aggregated token usage, cost, request counts, success rates, and latency percentiles. Specify start and end times for the desired period. ```bash curl -s "http://localhost:8000/api/admin/logs/stats?\nstart_time=2024-01-01T00:00:00Z&\nend_time=2024-01-31T23:59:59Z" ``` -------------------------------- ### Backend Development Commands Source: https://github.com/mylxsw/llm-gateway/blob/main/AGENTS.md Commands to set up a virtual environment, install dependencies, and run the FastAPI API service for the backend. Also includes running backend tests. ```bash cd backend uv venv .venv source .venv/bin/activate uv pip install -r requirements.txt uv run python -m app.main pytest ``` -------------------------------- ### Upload File and Create Cache in Go Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/caching-spec.md Initializes a client, uploads a file, and creates a cache with the file's content and a system instruction. It then retrieves the cache and generates content using it. ```go ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GEMINI_API_KEY"), Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } modelName := "gemini-1.5-flash-001" document, err := client.Files.UploadFromPath( ctx, filepath.Join(getMedia(), "a11.txt"), &genai.UploadFileConfig{ MIMEType : "text/plain", }, ) if err != nil { log.Fatal(err) } parts := []*genai.Part{ genai.NewPartFromURI(document.URI, document.MIMEType), } contents := []*genai.Content{ genai.NewContentFromParts(parts, genai.RoleUser), } cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{ Contents: contents, SystemInstruction: genai.NewContentFromText( "You are an expert analyzing transcripts.", genai.RoleUser, ), }) if err != nil { log.Fatal(err) } cacheName := cache.Name // Later retrieve the cache. cache, err = client.Caches.Get(ctx, cacheName, &genai.GetCachedContentConfig{}) if err != nil { log.Fatal(err) } response, err := client.Models.GenerateContent( ctx, modelName, genai.Text("Find a lighthearted moment from this transcript"), &genai.GenerateContentConfig{ CachedContent: cache.Name, }, ) if err != nil { log.Fatal(err) } fmt.Println("Response from cache (create from name):") printResponse(response) ``` -------------------------------- ### Configure Model Parameters in Node.js Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/generate-content-spec.md This Node.js example demonstrates how to set generation configuration parameters such as candidate count, stop sequences, and temperature. Make sure to install the '@google/generativeai' package and set your GEMINI_API_KEY environment variable. ```javascript // Make sure to include the following import: // import {GoogleGenAI} from '@google/genai'; const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); const response = await ai.models.generateContent({ model: "gemini-2.0-flash", contents: "Tell me a story about a magic backpack.", config: { candidateCount: 1, stopSequences: ["x"], maxOutputTokens: 20, temperature: 1.0, }, }); console.log(response.text) ``` -------------------------------- ### Style Transfer Example Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/image-generation.md An example prompt template for performing style transfer on an image. ```APIDOC ## Style Transfer ### Description This section provides a template and an example prompt for using the image generation model to perform style transfer, recreating an image in a different artistic style. ### Template ``` Transform the provided photograph of [subject] into the artistic style of [artist/art style]. Preserve the original composition but render it with [description of stylistic elements]. ``` ### Prompt Example ``` "Transform the provided photograph of a modern city street at night into the artistic style of Vincent van Gogh's 'Starry Night'. Preserve the original composition of buildings and cars, but render all elements with swirling, impasto brushstrokes and a dramatic palette of deep blues and bright yellows." ``` ``` -------------------------------- ### Generate Content - Node.js Example Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/generate-content-spec.md Example of how to generate content using the Gemini API in Node.js. ```Node.js // Make sure to include the following import: // import {GoogleGenAI} from '@google/genai'; const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); const response = await ai.models.generateContent({ model: "gemini-2.0-flash", contents: "Write a story about a magic backpack.", }); console.log(response.text); ``` -------------------------------- ### Generate Image Content in Go Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/image-generation.md This Go example demonstrates image generation using the `genai` package. It reads an image file, prepares it with a text prompt, and sends it to the Gemini API for content generation. ```go package main import ( "context" "fmt" "log" "os" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, nil) if err != nil { log.Fatal(err) } imagePath := "/path/to/cat_image.png" imgData, _ := os.ReadFile(imagePath) parts := []*genai.Part{ genai.NewPartFromText("Create a picture of my cat eating a nano-banana in a fancy restaurant under the Gemini constellation"), &genai.Part{ InlineData: &genai.Blob{ MIMEType: "image/png", Data: imgData, }, }, } contents := []*genai.Content{ genai.NewContentFromParts(parts, genai.RoleUser), } result, _ := client.Models.GenerateContent( ctx, "gemini-2.5-flash-image", contents, ) for _, part := range result.Candidates[0].Content.Parts { if part.Text != "" { fmt.Println(part.Text) } else if part.InlineData != nil { imageBytes := part.InlineData.Data outputFilename := "gemini_generated_image.png" _ = os.WriteFile(outputFilename, imageBytes, 0644) } } } ``` -------------------------------- ### Install Dev Dependencies Source: https://github.com/mylxsw/llm-gateway/blob/main/llm_api_converter/README.md Installs development dependencies for the project using pip. Ensure you are in the correct directory. ```bash pip install -e ".[dev]" ``` -------------------------------- ### Go SDK Example for Caching Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/caching-spec.md This Go code snippet demonstrates how to use the genai client to upload a file, create a cached content entry, and then retrieve it. ```APIDOC ## Go SDK Example This example shows how to use the Go SDK to interact with the caching API. ### Initialize Client ```go ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GEMINI_API_KEY"), Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } ``` ### Upload File ```go modelName := "gemini-1.5-flash-001" document, err := client.Files.UploadFromPath( ctx, filepath.Join(getMedia(), "a11.txt"), &genai.UploadFileConfig{ MIMEType : "text/plain", }, ) if err != nil { log.Fatal(err) } ``` ### Create Cached Content ```go parts := []*genai.Part{ genai.NewPartFromURI(document.URI, document.MIMEType), } contents := []*genai.Content{ genai.NewContentFromParts(parts, genai.RoleUser), } cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{ Contents: contents, SystemInstruction: genai.NewContentFromText( "You are an expert analyzing transcripts.", genai.RoleUser, ), }) if err != nil { log.Fatal(err) } ``` ### Get Cached Content ```go cache, err = client.Caches.Get(ctx, cache.Name, &genai.GetCachedContentConfig{}) if err != nil { log.Fatal(err) } fmt.Println("Retrieved cache:") fmt.Println(cache) ``` ``` -------------------------------- ### Generate Image Content in Go Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/image-generation.md This Go example shows how to generate an image using the `gemini-2.5-flash-image` model. It initializes the client, sends the content prompt, and processes the response to save the image data. ```go package main import ( "context" "fmt" "log" "os" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, nil) if err != nil { log.Fatal(err) } result, _ := client.Models.GenerateContent( ctx, "gemini-2.5-flash-image", genai.Text("A kawaii-style sticker of a happy red panda wearing a tiny bamboo hat. It's munching on a green bamboo leaf. The design features bold, clean outlines, simple cel-shading, and a vibrant color palette. The background must be white."), ) for _, part := range result.Candidates[0].Content.Parts { if part.Text != "" { fmt.Println(part.Text) } else if part.InlineData != nil { imageBytes := part.InlineData.Data outputFilename := "red_panda_sticker.png" _ = os.WriteFile(outputFilename, imageBytes, 0644) } } } ``` -------------------------------- ### API Response Example with 'is_stream' Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/changes/TROUBLESHOOTING_STREAM_DISPLAY.md This is an example of the JSON response from the /admin/logs API endpoint, showing the 'is_stream' field. ```json { "items": [ { "id": 123, "is_stream": true, ← Should be here! ... } ] } ``` -------------------------------- ### Generate Content (Go SDK) Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/image-generation.md Go SDK example for generating images. Demonstrates calling `GenerateContent` with image-specific configurations like aspect ratio and size. ```APIDOC ## Go SDK Examples ### Description Examples of using the Go SDK to generate content, focusing on image generation with configurable aspect ratios and sizes. ### Models - `gemini-2.5-flash-image` - `gemini-3-pro-image-preview` ### Usage ```go import ( // ... other imports genai "github.com/google/generative-ai-go/genai" "context" ) // Assuming 'ctx' is a context and 'client' is an initialized GenerativeModel client // For gemini-2.5-flash-image result, _ := client.Models.GenerateContent( ctx, "gemini-2.5-flash-image", genai.Text("Create a picture of a nano banana dish in a " + " fancy restaurant with a Gemini theme"), &genai.GenerateContentConfig{ ImageConfig: &genai.ImageConfig{ AspectRatio: "16:9", }, } ) // For gemini-3-pro-image-preview result_gemini3, _ := client.Models.GenerateContent( ctx, "gemini-3-pro-image-preview", genai.Text("Create a picture of a nano banana dish in a " + " fancy restaurant with a Gemini theme"), &genai.GenerateContentConfig{ ImageConfig: &genai.ImageConfig{ AspectRatio: "16:9", ImageSize: "2K", }, } ) ``` ### Parameters - **ctx** (context.Context) - The context for the request. - **modelName** (string) - The name of the model to use (e.g., `"gemini-2.5-flash-image"`). - **prompt** (genai.Part) - The prompt or content to send to the model. - **config** (*genai.GenerateContentConfig) - Configuration for content generation. - **ImageConfig** (*genai.ImageConfig) - Configuration specific to image generation. - **AspectRatio** (string) - Optional. The desired aspect ratio of the output image (e.g., `"16:9"`, `"1:1"`). Defaults to matching input image or 1:1. - **ImageSize** (string) - Optional. The desired size of the output image (e.g., `"2K"`). Only applicable for certain models like `gemini-3-pro-image-preview`. ``` -------------------------------- ### Frontend Development Commands Source: https://github.com/mylxsw/llm-gateway/blob/main/AGENTS.md Commands to start the frontend development server, build for production, and run ESLint checks for the frontend application. ```bash cd frontend npm run dev npm run build npm run lint ``` -------------------------------- ### Generate Image with Prompt and Multiple Images in Go Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/image-generation.md Utilize the `gemini-3-pro-image-preview` model in Go to generate an image from a text prompt and multiple image files. This example reads image files, constructs the content parts, and specifies generation configuration including aspect ratio and resolution. It then processes the response, printing text and saving generated images. ```go package main import ( "context" "fmt" "log" "os" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, nil) if err != nil { log.Fatal(err) } defer client.Close() model := client.GenerativeModel("gemini-3-pro-image-preview") model.GenerationConfig = &pb.GenerationConfig{ ResponseModalities: []pb.ResponseModality{genai.Text, genai.Image}, ImageConfig: &pb.ImageConfig{ AspectRatio: "5:4", ImageSize: "2K", }, } img1, err := os.ReadFile("person1.png") if err != nil { log.Fatal(err) } img2, err := os.ReadFile("person2.png") if err != nil { log.Fatal(err) } img3, err := os.ReadFile("person3.png") if err != nil { log.Fatal(err) } img4, err := os.ReadFile("person4.png") if err != nil { log.Fatal(err) } img5, err := os.ReadFile("person5.png") if err != nil { log.Fatal(err) } parts := []genai.Part{ genai.Text("An office group photo of these people, they are making funny faces."), genai.ImageData{MIMEType: "image/png", Data: img1}, genai.ImageData{MIMEType: "image/png", Data: img2}, genai.ImageData{MIMEType: "image/png", Data: img3}, genai.ImageData{MIMEType: "image/png", Data: img4}, genai.ImageData{MIMEType: "image/png", Data: img5}, } resp, err := model.GenerateContent(ctx, parts...) if err != nil { log.Fatal(err) } for _, part := range resp.Candidates[0].Content.Parts { if txt, ok := part.(genai.Text); ok { fmt.Printf("%s", string(txt)) } else if img, ok := part.(genai.ImageData); ok { err := os.WriteFile("office.png", img.Data, 0644) if err != nil { log.Fatal(err) } } } } ``` -------------------------------- ### Generate Content with Go SDK Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/image-generation.md Use the Go SDK to generate content, including images. This example demonstrates creating a logo and saving it to a file. Ensure the GOOGLE_API_KEY environment variable is set. ```go package main import ( "context" "fmt" "log" "os" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, nil) if err != nil { log.Fatal(err) } result, _ := client.Models.GenerateContent( ctx, "gemini-3-pro-image-preview", genai.Text("Create a modern, minimalist logo for a coffee shop called 'The Daily Grind'. The text should be in a clean, bold, sans-serif font. The color scheme is black and white. Put the logo in a circle. Use a coffee bean in a clever way."), &genai.GenerateContentConfig{ ImageConfig: &genai.ImageConfig{ AspectRatio: "1:1", }, }, ) for _, part := range result.Candidates[0].Content.Parts { if part.Text != "" { fmt.Println(part.Text) } else if part.InlineData != nil { imageBytes := part.InlineData.Data outputFilename := "logo_example.jpg" _ = os.WriteFile(outputFilename, imageBytes, 0644) } } } ``` -------------------------------- ### Upload File and Generate Content with Cache (Go) Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/generate-content-spec.md Shows how to upload a file and use it to create a cache for generating content with a Go client. Requires the GEMINI_API_KEY environment variable. ```go ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GEMINI_API_KEY"), Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } modelName := "gemini-1.5-flash-001" document, err := client.Files.UploadFromPath( ctx, filepath.Join(getMedia(), "a11.txt"), &genai.UploadFileConfig{ MIMEType : "text/plain", }, ) if err != nil { log.Fatal(err) } parts := []*genai.Part{ genai.NewPartFromURI(document.URI, document.MIMEType), } contents := []*genai.Content{ genai.NewContentFromParts(parts, genai.RoleUser), } cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{ Contents: contents, SystemInstruction: genai.NewContentFromText( "You are an expert analyzing transcripts.", genai.RoleUser, ), }) if err != nil { log.Fatal(err) } fmt.Println("Cache created:") fmt.Println(cache) // Use the cache for generating content. response, err := client.Models.GenerateContent( ctx, modelName, genai.Text("Please summarize this transcript"), &genai.GenerateContentConfig{ CachedContent: cache.Name, }, ) if err != nil { log.Fatal(err) } printResponse(response); ``` -------------------------------- ### Image Edit API Request Example Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/openai/image_edit_api_spec.md Example of how to make a request to the image edit API using curl. Ensure your API key is set in the Authorization header. ```http curl https://api.openai.com/v1/images/edits \ -H 'Content-Type: application/json' \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "images": [ { "image_url": "https://example.com/source-image.png" } ], "prompt": "Add a watercolor effect to this image", "background": "transparent", "model": "gpt-image-1.5", "moderation": "auto", "n": 1, "output_compression": 100, "output_format": "png", "partial_images": 1, "quality": "high", "size": "1024x1024", "user": "user-1234" }' ``` -------------------------------- ### Run Backend Tests Source: https://github.com/mylxsw/llm-gateway/blob/main/README.md Navigate to the backend directory and execute this command to run the project's test suite. ```bash cd backend pytest ``` -------------------------------- ### Corrected API Response Example Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/changes/FIX_LOG_SERVICE_CRITICAL.md This JSON example shows the expected API response after the fix, where the 'is_stream' field now correctly reflects the value stored in the database (e.g., 'true'). ```json { "items": [ { "id": 123, "is_stream": true, // ← Now correctly reflects database value! "request_time": "...", ... } ] } ``` -------------------------------- ### Generate Content from Audio File (Go) Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/generate-content-spec.md Uploads an audio file and generates a summary using the Go SDK. Ensure the GEMINI_API_KEY environment variable is set and the audio file exists. ```Go ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GEMINI_API_KEY"), Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } file, err := client.Files.UploadFromPath( ctx, filepath.Join(getMedia(), "sample.mp3"), &genai.UploadFileConfig{ MIMEType : "audio/mpeg", }, ) if err != nil { log.Fatal(err) } parts := []*genai.Part{ genai.NewPartFromText("Give me a summary of this audio file."), genai.NewPartFromURI(file.URI, file.MIMEType), } contents := []*genai.Content{ genai.NewContentFromParts(parts, genai.RoleUser), } for result, err := range client.Models.GenerateContentStream( ctx, "gemini-2.0-flash", contents, nil, ) { if err != nil { log.Fatal(err) } fmt.Print(result.Candidates[0].Content.Parts[0].Text) } ``` -------------------------------- ### Style Transfer Prompt Example Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/image-generation.md An example prompt demonstrating how to instruct the model to perform style transfer. It specifies the subject, the target artistic style, and details about the desired stylistic elements. ```plaintext "Transform the provided photograph of a modern city street at night into the artistic style of Vincent van Gogh's 'Starry Night'. Preserve the original composition of buildings and cars, but render all elements with swirling, impasto brushstrokes and a dramatic palette of deep blues and bright yellows." ``` -------------------------------- ### List Models (Go SDK) Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/models-spec.md Example of how to list models and filter them by supported actions (e.g., generateContent, embedContent) using the Go SDK. ```APIDOC ## Client.Models.List ### Description Retrieves a list of models available through the client. This method allows filtering models based on their supported actions. ### Method Signature `client.Models.List(ctx context.Context, config *genai.ListModelsConfig) (*genai.ListModelsResponse, error)` ### Parameters - **ctx** (context.Context) - The context for the request. - **config** (*genai.ListModelsConfig) - Configuration for listing models. Can be nil or an empty struct to retrieve all models. ### Response - **models** (*genai.ListModelsResponse) - Contains a list of `Model` objects. - **Items** ([]Model) - The list of models. - **Name** (string) - The name of the model. - **SupportedActions** ([]string) - A list of actions the model supports (e.g., "generateContent", "embedContent"). - **err** (error) - An error if the request fails. ### Example Usage ```go ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GEMINI_API_KEY"), Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } models, err := client.Models.List(ctx, &genai.ListModelsConfig{}) if err != nil { log.Fatal(err) } fmt.Println("List of models that support generateContent:") for _, m := range models.Items { for _, action := range m.SupportedActions { if action == "generateContent" { fmt.Println(m.Name) break } } } ``` ``` -------------------------------- ### Function Parameters JSON Schema Example Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/caching-spec.md An example of a JSON schema used to describe function parameters. This schema defines the structure, types, and required fields for the function's input. ```json { "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" } }, "additionalProperties": false, "required": ["name", "age"], "propertyOrdering": ["name", "age"] } ``` -------------------------------- ### Create and Use Cached Content in Go Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/google/caching-spec.md Demonstrates uploading a file, creating a cached content entry with a system instruction, and then using that cache to generate content. Ensure the GEMINI_API_KEY environment variable is set. ```go ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GEMINI_API_KEY"), Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } modelName := "gemini-1.5-flash-001" document, err := client.Files.UploadFromPath( ctx, filepath.Join(getMedia(), "a11.txt"), &genai.UploadFileConfig{ MIMEType : "text/plain", }, ) if err != nil { log.Fatal(err) } parts := []*genai.Part{ genai.NewPartFromURI(document.URI, document.MIMEType), } contents := []*genai.Content{ genai.NewContentFromParts(parts, genai.RoleUser), } cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{ Contents: contents, SystemInstruction: genai.NewContentFromText( "You are an expert analyzing transcripts.", genai.RoleUser, ), }) if err != nil { log.Fatal(err) } fmt.Println("Cache created:") fmt.Println(cache) // Use the cache for generating content. response, err := client.Models.GenerateContent( ctx, modelName, genai.Text("Please summarize this transcript"), &genai.GenerateContentConfig{ CachedContent: cache.Name, }, ) if err != nil { log.Fatal(err) } printResponse(response) ``` -------------------------------- ### Get Log Details Source: https://github.com/mylxsw/llm-gateway/blob/main/docs/api.md Retrieves the details of a specific request log by its ID. ```APIDOC ## GET /admin/logs/{id} ### Description Get Log Details ### Method GET ### Endpoint /admin/logs/{id} ### Parameters #### Path Parameters - **id** (int) - Required - The ID of the log to retrieve. ### Response #### Success Response (200) - **id** (int) - Log entry ID. - **request_time** (string) - Timestamp of the request. - **api_key_id** (int) - ID of the API key used. - **api_key_name** (string) - Name of the API key used. - **requested_model** (string) - The model requested by the client. - **target_model** (string) - The model ultimately used by the provider. - **provider_id** (int) - ID of the provider. - **provider_name** (string) - Name of the provider. - **retry_count** (int) - Number of retries performed. - **first_byte_delay_ms** (int) - Time in milliseconds until the first byte of the response was received. - **total_time_ms** (int) - Total time in milliseconds for the request. - **input_tokens** (int) - Number of input tokens. - **output_tokens** (int) - Number of output tokens. - **request_headers** (object) - Headers of the request. - **request_body** (object) - Body of the request. - **response_status** (int) - HTTP status code of the response. - **response_body** (object) - Body of the response. - **error_info** (object) - Information about any errors encountered. - **trace_id** (string) - Unique identifier for tracing the request. #### Response Example ```json { "id": 1, "request_time": "2024-01-10T12:00:00Z", "api_key_id": 1, "api_key_name": "Production Key", "requested_model": "gpt-4", "target_model": "gpt-4-0613", "provider_id": 1, "provider_name": "OpenAI Official", "retry_count": 0, "first_byte_delay_ms": 500, "total_time_ms": 2000, "input_tokens": 100, "output_tokens": 50, "request_headers": { "authorization": "Bearer lgw-***...***", "content-type": "application/json", "user-agent": "OpenAI/Python" }, "request_body": { "model": "gpt-4", "messages": [{"role": "user", "content": "Hello"}] }, "response_status": 200, "response_body": { "id": "chatcmpl-xxx", "choices": [...] }, "error_info": null, "trace_id": "trace-xxx" } ``` ```