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
The handler reads the OpenAI API key from the mounted secret and uses it to create a chat completion. The request body is sent as the user message.
765
+
766
+
The OpenAI client is initialised once on the first request and stored in a global variable. This means subsequent invocations reuse the same client, avoiding the overhead of creating a new connection on every request.
767
+
768
+
```python
769
+
from openai import OpenAI
770
+
771
+
client = None
772
+
773
+
def initClient():
774
+
apiKey = read_secret('openai-api-key')
775
+
return OpenAI(api_key=apiKey)
776
+
777
+
def handle(event, context):
778
+
global client
779
+
780
+
# Initialise the client once and reuse it across invocations
with open("/var/openfaas/secrets/" + name, "r") as f:
798
+
return f.read().strip()
799
+
```
800
+
801
+
**6. Deploy and invoke**
802
+
803
+
Build, push and deploy the function with `faas-cli up`. The `--filter` flag selects a single function from the stack file and `--tag digest` uses the image content hash as the tag instead of `latest`, so that Kubernetes always pulls an updated image:
804
+
805
+
```bash
806
+
faas-cli up \
807
+
--filter openai-chat \
808
+
--tag digest
809
+
810
+
# Send a prompt to the function
811
+
curl http://127.0.0.1:8080/function/openai-chat \
812
+
--data "What is the capital of France?"
813
+
```
814
+
815
+
!!! tip "Streaming responses with Server-Sent Events (SSE)"
816
+
817
+
This example waits for the full completion before responding. To stream tokens back to the client as they are generated, you can use Server-Sent Events (SSE) with the `python3-flask` template, which gives direct access to Flask's `stream_with_context` helper. See [Stream OpenAI responses from functions using Server Sent Events](https://www.openfaas.com/blog/openai-streaming-responses/) on the OpenFaaS blog for a working example.
818
+
712
819
## OpenTelemetry zero-code instrumentation
713
820
714
821
Using [OpenTelemetry zero-code instrumentation](https://opentelemetry.io/docs/zero-code/python/) for python functions requires some minor modifications to the existing Python templates.
0 commit comments