You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 20, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: docs/guides/python/serverless-ai-api.mdx
+9-10Lines changed: 9 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -101,7 +101,7 @@ Our goal is to create an HTTP endpoint `/summarize` (for example) that accepts s
101
101
102
102
2.**Import Nitric and dependencies:** We'll need Nitric's API utilities and the OpenAI library (for AI). Make sure you add `openai` to your dependencies (`uv add openai`). Then, in `summarize.py`:
103
103
104
-
```python
104
+
```python title:services/summarize.py
105
105
import os
106
106
from openai import OpenAI
107
107
from nitric.resources import api
@@ -113,7 +113,7 @@ Here we import `api` to create an API resource, `Nitric` to run the app, and `Ht
113
113
114
114
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:
115
115
116
-
```python
116
+
```python title:services/summarize.py
117
117
# Configure OpenAI API key from env
118
118
api_key = os.environ.get("OPENAI_API_KEY")
119
119
ifnot api_key:
@@ -122,9 +122,9 @@ if not api_key:
122
122
123
123
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.)
124
124
125
-
4.**Create the OpenAI client:**Now, use Nitric to create a POST route for summarization:
125
+
4.**Create the OpenAI client:**Initialise the `OpenAI` client using the fetched API key.
126
126
127
-
```python
127
+
```python title:services/summarize.py
128
128
# Create OpenAI client
129
129
# This could be a local model during development if you have Ollama or similar installed.
130
130
# For example, if you have Ollama running locally, you could use:
@@ -182,15 +181,15 @@ _A note on async:_ We defined the handler as `async def`. The Nitric framework s
182
181
183
182
5.**Finalize the Nitric app run:** After defining all your routes and handlers in the file, make sure to add:
184
183
185
-
```python
184
+
```python title:services/summarize.py
186
185
Nitric.run()
187
186
```
188
187
189
188
This call tells Nitric to start the application (it will scan for all defined services/routes and run the local server when invoked via `nitric start`). In our case, it picks up `summarize_api` and the attached route.
190
189
191
190
That's it for our service code! We have a complete Python backend that will accept text and return an AI-generated summary. The entire `summarize.py` file should look like this (for reference):
192
191
193
-
```python{{ title:services/summarize.py }}
192
+
```python title:services/summarize.py
194
193
import os
195
194
from openai import OpenAI
196
195
from nitric.resources import api
@@ -318,7 +317,7 @@ curl -X POST https://<<your-api-url>>/summarize \
318
317
-d '{"text": "OpenAI API integration with serverless functions is very powerful, allowing applications to leverage AI without managing infrastructure."}'
319
318
```
320
319
321
-
_(Make sure to put your actual endpoint URL in place of `<<your-api-url>>`._
320
+
_Make sure to put your actual endpoint URL in place of `<<your-api-url>>`._
322
321
323
322
You should get a JSON response with a summary, similar to what you saw locally. Congratulations - your AI-powered summarization service is now live on the internet, running completely on serverless infrastructure! 🎉
324
323
@@ -348,6 +347,6 @@ All of this was done without needing to be AWS experts or know how to set up API
348
347
- Add other Nitric resources: need a [database](/sql) or [key-value store](/keyvalue) to cache results? Nitric offers `sql()` and `kv()` resources that can be used similarly to how we defined the API. Want to trigger actions on a schedule (cron jobs) or handle file storage in the cloud? Nitric has primitives for [schedules](/schedules), [buckets](/storage) (object storage), [queues](/messaging#queues), pub-sub [topics](/messaging#topics), and more - all accessible through simple code constructs, and all cloud-agnostic. (For example, you could schedule a daily job to fetch some data and summarize it, using Nitric's `schedule` resource.)
349
348
- Deploy to another cloud: Try deploying the same app to Azure or GCP. It should be as easy as creating a new stack for that provider. This can be a great way to learn differences in cloud offerings without changing your code - Nitric will map your API to the equivalent service (e.g., Azure Container Apps + API Management, or GCP Cloud Run + API Gateway).
350
349
351
-
As you continue exploring, check out the official Nitric documentation and examples for deeper dives. The [Nitric docs site](https://nitric.io/docs) has guides on all resource types and features (for instance, how to use **Queues/Topics for event-driven patterns**, or how to secure your API with auth). There's also a community [Discord](https://nitric.io/chat) and more examples on GitHub if you run into questions.
350
+
As you continue exploring, check out the official Nitric documentation and examples for deeper dives. The [Nitric docs site](https://nitric.io/docs) has guides on all resource types and features (for instance, how to use **Queues/Topics for event-driven patterns**, or how to secure your API with auth). There's also a community [Discord](https://nitric.io/chat) and more [examples](https://github.com/nitrictech/examples) on GitHub if you want inspiration.
352
351
353
352
We hope this tutorial showed you that modern cloud development doesn't have to be intimidating. Using Nitric, you offloaded the undifferentiated heavy lifting of cloud setup and got straight to building something useful. Happy coding, and may all your deployments be this smooth! 🚀
0 commit comments