Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit bf86cb2

Browse files
committed
update code examples
1 parent 8d38f2b commit bf86cb2

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

docs/guides/python/serverless-ai-api.mdx

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Our goal is to create an HTTP endpoint `/summarize` (for example) that accepts s
9393

9494
```python
9595
import os
96-
import openai
96+
from openai import OpenAI
9797
from nitric.resources import api
9898
from nitric.application import Nitric
9999
from nitric.context import HttpContext
@@ -104,23 +104,31 @@ Here we import `api` to create an API resource, `Nitric` to run the app, and `Ht
104104
3. **Configure OpenAI API key:** It's best not to hardcode secrets. If you have your OpenAI API key, set it as an environment variable (e.g., create a .env file for the project containing `OPENAI_API_KEY=sk-...`). We can then configure the OpenAI client in code:
105105

106106
```python
107-
openai.api_key = os.environ.get("OPENAI_API_KEY")
108-
if not openai.api_key:
107+
# Configure OpenAI API key from env
108+
api_key = os.environ.get("OPENAI_API_KEY")
109+
if not api_key:
109110
raise Exception("Please set the OPENAI_API_KEY environment variable")
110111
```
111112

112113
This will fetch the API key from your environment. (If you prefer, Nitric also has a built-in **Secrets** feature to securely manage secrets, but for simplicity we'll stick to an env var here.)
113114

114-
4. **Define the API and route handler:** Now, use Nitric to create a POST route for summarization:
115+
4. **Create the OpenAI client:** Now, use Nitric to create a POST route for summarization:
115116

116117
```python
117-
# Create an API named 'main'
118+
# Create OpenAI client
119+
client = OpenAI(api_key=api_key)
120+
```
121+
122+
5. **Define the API and route handler:** Now, use Nitric to create a POST route for summarization:
123+
124+
```python
125+
# Create a Nitric API named 'main'
118126
summarize_api = api("main")
119127

120128
@summarize_api.post("/summarize")
121129
async def summarize_text(ctx: HttpContext) -> None:
122130
# Extract input text from request (assuming JSON body with a 'text' field)
123-
req_data = ctx.req.json # parses JSON body into a dict
131+
req_data = ctx.req.json
124132
if not req_data or 'text' not in req_data:
125133
ctx.res.status = 400
126134
ctx.res.body = {"error": "No text provided for summarization."}
@@ -129,18 +137,20 @@ async def summarize_text(ctx: HttpContext) -> None:
129137
text_to_summarize = req_data['text']
130138
# Call OpenAI API to get summary (this is a blocking call, for demo simplicity)
131139
try:
132-
response = openai.ChatCompletion.create(
140+
response = client.chat.completions.create(
133141
model="gpt-3.5-turbo",
134142
messages=[{"role": "user", "content": f"Summarize the following text:\n\n{text_to_summarize}"}],
135-
max_tokens=100
143+
max_tokens=50
136144
)
137145
# Extract the assistant's reply (summary text)
138-
summary = response['choices'][0]['message']['content']
146+
summary = response.choices[0].message.content
139147
# Set response
140148
ctx.res.body = {"summary": summary.strip()}
141149
except Exception as e:
142150
ctx.res.status = 500
143151
ctx.res.body = {"error": str(e)}
152+
153+
Nitric.run()
144154
```
145155

146156
Let's break down what's happening:
@@ -167,35 +177,42 @@ That's it for our service code! We have a complete Python backend that will acce
167177

168178
```python {{ title: services/summarize.py }}
169179
import os
170-
import openai
180+
from openai import OpenAI
171181
from nitric.resources import api
172182
from nitric.application import Nitric
173183
from nitric.context import HttpContext
174184

175185
# Configure OpenAI API key from env
176-
openai.api_key = os.environ.get("OPENAI_API_KEY")
177-
if not openai.api_key:
186+
api_key = os.environ.get("OPENAI_API_KEY")
187+
if not api_key:
178188
raise Exception("Please set the OPENAI_API_KEY environment variable")
179189

180-
# Define Nitric API
190+
# Create OpenAI client
191+
client = OpenAI(api_key=api_key)
192+
193+
# Create a Nitric API named 'main'
181194
summarize_api = api("main")
182195

183196
@summarize_api.post("/summarize")
184197
async def summarize_text(ctx: HttpContext) -> None:
198+
# Extract input text from request (assuming JSON body with a 'text' field)
185199
req_data = ctx.req.json
186200
if not req_data or 'text' not in req_data:
187201
ctx.res.status = 400
188202
ctx.res.body = {"error": "No text provided for summarization."}
189203
return
190204

191205
text_to_summarize = req_data['text']
206+
# Call OpenAI API to get summary (this is a blocking call, for demo simplicity)
192207
try:
193-
response = openai.ChatCompletion.create(
208+
response = client.chat.completions.create(
194209
model="gpt-3.5-turbo",
195210
messages=[{"role": "user", "content": f"Summarize the following text:\n\n{text_to_summarize}"}],
196-
max_tokens=100
211+
max_tokens=50
197212
)
198-
summary = response['choices'][0]['message']['content']
213+
# Extract the assistant's reply (summary text)
214+
summary = response.choices[0].message.content
215+
# Set response
199216
ctx.res.body = {"summary": summary.strip()}
200217
except Exception as e:
201218
ctx.res.status = 500

0 commit comments

Comments
 (0)