Files
home_stack/scripts/generate-ollama-client-simple.sh
furyhawk 196a573b8c feat: Generate TypeScript client for Ollama API using OpenAPI specification
- Added a new script `generate-ollama-client-simple.sh` for generating a simple TypeScript client using the existing OpenAPI tools.
- Created a comprehensive `generate-ollama-client.sh` script to generate a FastAPI client from the Ollama OpenAPI specification with support for axios.
- Introduced `generate_fastapi_client.py` to support generating FastAPI clients from OpenAPI specifications in multiple languages, including TypeScript and Python.
- Generated TypeScript types for the Ollama API endpoints in `types.gen.ts`.
- Included example usage files for both Python and TypeScript clients to demonstrate how to interact with the Ollama API.
2025-06-24 20:51:54 +08:00

143 lines
3.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Simple script to generate TypeScript client using the project's existing tools
# This uses @hey-api/openapi-ts which is already configured in the project
set -e
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
# Get script directory and project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
OLLAMA_OPENAPI_PATH="$PROJECT_ROOT/ai_stack/ollama/openapi.json"
OUTPUT_DIR="$PROJECT_ROOT/frontend/src/ollama-client"
echo -e "${GREEN}🚀 Generating Ollama TypeScript Client${NC}"
# Verify the OpenAPI file exists
if [ ! -f "$OLLAMA_OPENAPI_PATH" ]; then
echo -e "${RED}❌ Error: OpenAPI file not found at $OLLAMA_OPENAPI_PATH${NC}"
exit 1
fi
# Create output directory
mkdir -p "$OUTPUT_DIR"
# Change to frontend directory
cd "$PROJECT_ROOT/frontend"
echo -e "${GREEN}📋 Using OpenAPI spec: $OLLAMA_OPENAPI_PATH${NC}"
echo -e "${GREEN}📁 Output directory: $OUTPUT_DIR${NC}"
# Generate client using the project's existing openapi-ts setup
echo -e "${GREEN}⚙️ Generating TypeScript client...${NC}"
npx @hey-api/openapi-ts \
--input "$OLLAMA_OPENAPI_PATH" \
--output "$OUTPUT_DIR" \
--client "legacy/axios"
# Format the generated code with biome
if [ -f "biome.json" ]; then
echo -e "${GREEN}🎨 Formatting generated code...${NC}"
npx biome format --write "$OUTPUT_DIR"
fi
# Create a simple usage example
EXAMPLE_FILE="$OUTPUT_DIR/example.ts"
cat > "$EXAMPLE_FILE" << 'EOF'
/**
* Example usage of the generated Ollama FastAPI client
*/
import { client, OllamaService } from './index';
// Configure the base URL for your Ollama API
client.setConfig({
baseUrl: 'http://localhost:11434', // Default Ollama port
});
// Example: List available models
async function listModels() {
try {
const response = await OllamaService.listLocalModelsV1ModelsGet();
console.log('Available models:', response.data);
return response.data;
} catch (error) {
console.error('Error listing models:', error);
}
}
// Example: Get a specific model
async function getModel(modelId: string) {
try {
const response = await OllamaService.getLocalModelV1ModelsModelIdGet({
path: { model_id: modelId }
});
console.log('Model details:', response.data);
return response.data;
} catch (error) {
console.error('Error getting model:', error);
}
}
// Example: Chat completion
async function chatCompletion() {
try {
const response = await OllamaService.handleCompletionsV1ChatCompletionsPost({
body: {
model: 'llama2', // Replace with your model
messages: [
{
role: 'user',
content: 'Hello, how are you?'
}
]
}
});
console.log('Chat response:', response.data);
return response.data;
} catch (error) {
console.error('Error in chat completion:', error);
}
}
// Example: Transcribe audio
async function transcribeAudio(audioFile: File) {
try {
const formData = new FormData();
formData.append('file', audioFile);
formData.append('model', 'whisper-1');
const response = await OllamaService.transcribeFileV1AudioTranscriptionsPost({
body: formData
});
console.log('Transcription:', response.data);
return response.data;
} catch (error) {
console.error('Error transcribing audio:', error);
}
}
// Export the functions for use in your application
export {
listModels,
getModel,
chatCompletion,
transcribeAudio
};
EOF
echo -e "${GREEN}✅ Ollama TypeScript client generated successfully!${NC}"
echo -e "${GREEN}📍 Client available at: $OUTPUT_DIR${NC}"
echo -e "${GREEN}📝 Usage example: $EXAMPLE_FILE${NC}"
echo ""
echo -e "${YELLOW}💡 Quick start:${NC}"
echo -e "${YELLOW}import { listModels, chatCompletion } from './ollama-client/example';${NC}"
echo -e "${YELLOW}const models = await listModels();${NC}"
echo -e "${YELLOW}const response = await chatCompletion();${NC}"