<p>This is a sample demonstrating to use the <a href="https://platform.openai.com/docs/api-reference/chat/create">OpenAI Chat API</a> with TypeScript.</p> <p>The application exposes an endpoint to ask a question in the body of a POST request and get an answer in the response. Originally created for the <a href="https://github.com/fermyon/workshops/blob/main/spin/02b-json-api-openai.md">Spin Workshop</a></p> <h2>Pre-requisites</h2> <ul> <li>npm</li> <li>spin (&gt;1.3)</li> </ul> <h2>Getting Started</h2> <ul> <li>Clone the repository</li> <li>In the repository folder, run <code>npm install</code></li> <li>Generate an API key for OpenAI at <a href="https://platform.openai.com/account/api-keys">platform.openai.com/account/api-keys</a> </li> <li>Set an environment variable to hold the API key <br> <code>export SPIN_CONFIG_OPENAI_KEY=&quot;Your OpenAI Key&quot;</code></li> <li>Run Spin <br> <code>spin build --up</code></li> </ul> <h2>Running the Application with a Provided API Key</h2> <p>This example gets the OpenAI key from a Spin configuration variable. Pass the value of the key to Spin in an environment variable prefixed with SPIN_CONFIG. Spin’s environment variable provider with parse the local process environment for these variables and expose them through the Spin config SDK.</p> <pre><code>$ export SPIN_CONFIG_OPENAI_KEY=$YOUR_OPENAI_KEY $ spin build --up Serving http://127.0.0.1:3000 Available Routes: openai-chat-ts: http://127.0.0.1:3000/question </code></pre> <p>Now ask your question in the body of the request:</p> <pre><code>$ curl -d &quot;Will I get a pet tortoise?&quot; http://127.0.0.1:3000/question {&quot;answer&quot;: &quot;Outlook is uncertain.&quot;} </code></pre> <h2>Notes</h2> <ul> <li>A system prompt has been added to restrict the response to five words or less.</li> <li>No context preservation: This is a simple example that demonstrates how to use the OpenAI chat API in a Question-Answer pattern. <b>The conversation context is not preserved.</b></li> </ul> <h2>Code Overview</h2> <p>The HTTP request is handled by the <code>handleRequest</code> function. Following is an overview of the process flow:</p> <ul> <li>Retreive the question by decoding the response body</li> <li>Fetch the OpenAPI Key from the Spin Config <a href="https://developer.fermyon.com/spin/dynamic-configuration#custom-config-providers">Reference</a></li> <li>Structure the request to be sent to OpenAI </li> <li>Decode the response</li> <li>Structure and return the response to the client.</li> </ul> <p>Please refer to the code for the details of the implementation.</p> <h2>References</h2> <ul> <li> <p><a href="https://developer.fermyon.com/spin/javascript-components#sending-outbound-http-requests">Sending Outbound HTTP Requests</a></p> </li> <li> <p><a href="https://developer.fermyon.com/spin/dynamic-configuration#custom-config-providers">Spin Config</a></p> </li> <li> <p><a href="https://platform.openai.com/docs/api-reference/chat/create">Create chat completion</a></p> </li> </ul>