Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OpenAI APIで API Changelogを要約してみた話 / chatgpt-osaka-1

OpenAI APIで API Changelogを要約してみた話 / chatgpt-osaka-1

Hidetaka Okamoto

April 19, 2023
Tweet

More Decks by Hidetaka Okamoto

Other Decks in Programming

Transcript

  1. • Stripe Developer Advocate • WordCamp Kyoto 2017 • @hidetaka_dev

    • https://hidetaka.dev Ԭຊ लߴ (Hide) HELLO! ✋
  2. Cloudflare Workers༻ͷAPIϥούʔ import { CreateChatCompletionRequest, CreateChatCompletionResponse } from "openai"; import

    { FetchClient } from "./abstractFetchClient"; export class OpenAI extends FetchClient { public constructor(apiKey: string) { super("https://api.openai.com/v1", apiKey) } public async createChatCompletion(body: CreateChatCompletionRequest): Promise<CreateChatCompletionResponse> { const result = await this._fetch<CreateChatCompletionResponse>(`${this.baseURL}/chat/completions`, { method: "POST", body: JSON.stringify(body) }) return result } }
  3. Cloudflare KVͰཁ໿݁ՌΛอଘ if (!feed.id || !feed.title || !feed.description) return const

    itemId = feed.id const storeItem = await this.KV.get(itemId) const date = new Date(feed.title); const time = date.getTime() if (isNaN(time)) return if (storeItem) { const expireTargetDate = new Date() // ൒೥ΑΓલͳΒফ͢ if (calcMonthDiff(date, expireTargetDate) > -6) { return } // ExpireσʔλΛ࡟আ͢Δʢ͔̍Β൒೥͙Β͍ʁʣ await this.KV.delete(itemId) return } // Summarize text by openai API const summary = await this.summarizer.generateSummary(feed.description) // Save item to the KV await this.KV.put(itemId, summary || "No summary data.")
  4. Discord WebhookͰ௨஌ await fetch(this.DISCORD_WEBHOOK_URL, { method: "POST", headers: { "Content-Type":

    "application/json" }, body: JSON.stringify({ content: [ `**${feed.title}ͷAPIΞοϓσʔτ৘ใ by ChatGPT**`, summary, feed.link ].filter(Boolean) .join('\n') }) })