0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Geminiで生成した定義からOpenAPI codegenでコード生成しKotlin Multiplatformで使用する件

Last updated at Posted at 2024-12-17

概要

(自分メモ)
実際の要求と応答をGeminiに与えopenapi定義ファイルを生成し、swagger codegenでクライアントコードを生成しkotlinで使用する例
楽だわ

環境

  • Windows 11
  • Gemini 2.0 flash (Gemini API Key を用意) ※APIではgemini-1.5-flashを使用
  • Java 21

OpenAPI定義ファイル作成

Geminiに自身のQuick Startページから定義を作るよう依頼し..
下記要求と応答を表現するopenapi3.0.yamlを生成して
- データ型はschemas:として定義
- keyは全API共通なのでのsecurity:に定義

要求:
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY" \
-H 'Content-Type: application/json' \
-d '{"contents": [{"parts":[{"text": "Write a story about a magic backpack."}]}]}'

応答:
{
  "candidates": 
  # ..中略..
  "modelVersion": "gemini-1.5-flash"
}
...て作ってもらった gemini.yml
openapi: 3.0.0
info:
  title: Gemini API
  version: v1beta
servers:
  - url: 'https://generativelanguage.googleapis.com/v1beta'
components:
  schemas:
    Part:
      type: object
      properties:
        text:
          type: string
    Content:
      type: object
      properties:
        parts:
          type: array
          items:
            $ref: '#/components/schemas/Part'
        role:
          type: string
    Candidate:
      type: object
      properties:
        content:
          $ref: '#/components/schemas/Content'
        finishReason:
          type: string
        avgLogprobs:
          type: number
          format: float
    UsageMetadata:
      type: object
      properties:
        promptTokenCount:
          type: integer
        candidatesTokenCount:
          type: integer
        totalTokenCount:
          type: integer
    GenerateContentRequest:
      type: object
      properties:
        contents:
          type: array
          items:
            type: object
            properties:
              parts:
                type: array
                items:
                  $ref: '#/components/schemas/Part'
    GenerateContentResponse:
      type: object
      properties:
        candidates:
          type: array
          items:
            $ref: '#/components/schemas/Candidate'
        usageMetadata:
          $ref: '#/components/schemas/UsageMetadata'
        modelVersion:
          type: string
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: query
      name: key
paths:
  '/models/gemini-1.5-flash:generateContent':
    post:
      description: desc
      summary: Generates content using the Gemini model.
      operationId: generateContent # ※
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerateContentRequest'
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GenerateContentResponse'
      security: []
      parameters:
        - schema:
            type: string
          in: query
          name: key
          required: true
※一部呼び出し側に合うよう調整
  '/models/gemini-1.5-flash:generateContent':
    post:
      description: desc
      summary: Generates content using the Gemini model.
+     operationId: generateContent

Swaggerに突っ込んで実行できることを確認

ソース

App.kt(抜粋)
expect val GEMINI_API_KEY: String

suspend fun appGemini() {
    val client = HttpClient { install(ContentNegotiation) { json(Json) } }
    val api = DefaultApi("https://generativelanguage.googleapis.com/v1beta", client)
    val prompt = "GeminiとOpenAPIに関する今日のTipsを一つ教えて"
    val req = GenerateContentRequest(listOf(GenerateContentRequestContentsInner(listOf(Part(prompt)))))
    val res = api.generateContent(GEMINI_API_KEY, req).body()
    println(Json.encodeToString(res.candidates?.get(0)?.content?.parts?.get(0)?.text))
    client.close()
}

全ソース

テスト実行

GEMINI_API_KEY='AIza************'
sh gradlew jvmRun -DmainClass=MainKt  # jvmの場合
sh gradlew jsNodeRun                  # node.jsの場合

Build & Run


(メモ)ローカルでSwagger Editor

wget https://github.com/swagger-api/swagger-editor/archive/refs/tags/v4.14.1.zip
unzip v4.14.1.zip

ブラウザに./index.htmlを放り込む

参考

OpenAPI Generator
Gradleプラグイン

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?