-
Notifications
You must be signed in to change notification settings - Fork 31
genai-function-calling: add openai-agents python via OpenInference #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
01785c4
genai-function-calling: add openai-agents via OpenInference
codefromthecrypt d9fd268
fixed context
codefromthecrypt 22d8d6b
update
codefromthecrypt dae8a1f
azure
codefromthecrypt 776ced5
Adds tests
codefromthecrypt e7b9cfa
syntax
codefromthecrypt 1674686
remove temporary code
codefromthecrypt 7b447d1
explain
codefromthecrypt 8687961
readme-and-shiny-screenshot
codefromthecrypt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| name: test-genai-function-calling | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: | ||
| - main | ||
| paths: | ||
| - 'genai-function-calling/openai-agents/**' | ||
| - '!**/*.md' | ||
| - '!**/*.png' | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-24.04 | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Python 3.12 | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: 3.12 | ||
|
|
||
| - name: openai-agents | ||
| run: | | ||
| pip install -r requirements.txt | ||
| pip install -r requirements-dev.txt | ||
| pytest --vcr-record=none | ||
| working-directory: genai-function-calling/openai-agents |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Use glibc-based image with pre-compiled wheels for psutil | ||
| FROM python:3.12-slim | ||
|
|
||
| # TODO: temporary until openai-agents 0.0.5 | ||
| RUN apt-get update \ | ||
| && apt-get install -y --no-install-recommends git \ | ||
| && apt-get clean \ | ||
| && rm -rf /var/lib/apt/lists/* | ||
|
|
||
| RUN --mount=type=cache,target=/root/.cache/pip python -m pip install --upgrade pip | ||
|
|
||
| COPY requirements.txt /tmp | ||
| RUN --mount=type=cache,target=/root/.cache/pip pip install -r /tmp/requirements.txt | ||
| RUN --mount=type=cache,target=/root/.cache/pip edot-bootstrap --action=install | ||
| # TODO(EDOT): remove when > v0.6.1 | ||
| RUN pip uninstall -y elastic-opentelemetry-instrumentation-openai \ | ||
| && apt-get update \ | ||
| && apt-get install -y --no-install-recommends git \ | ||
| && apt-get clean \ | ||
| && rm -rf /var/lib/apt/lists/* \ | ||
| && pip uninstall -y elastic-opentelemetry-instrumentation-openai \ | ||
| && pip install 'elastic-opentelemetry-instrumentation-openai @ git+https://github.com/elastic/elastic-otel-python-instrumentations.git@main#subdirectory=instrumentation/elastic-opentelemetry-instrumentation-openai' | ||
|
|
||
| COPY main.py / | ||
|
|
||
| CMD [ "python", "main.py" ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # Function Calling with OpenAI Agents SDK (Python) | ||
|
|
||
| [main.py](main.py) implements the [example application flow][flow] using | ||
| [OpenAI Agents SDK (Python)][openai-agents-python]. | ||
|
|
||
| [Dockerfile](Dockerfile) starts the application with Elastic Distribution | ||
| of OpenTelemetry (EDOT) Python, via `opentelemetry-instrument`. | ||
|
|
||
| Notably, this shows how to add extra instrumentation to EDOT, as the OpenAI | ||
| Agents support is via [OpenInference][openinference]. | ||
|
|
||
| ## Configure | ||
|
|
||
| Copy [env.example](env.example) to `.env` and update its `OPENAI_API_KEY`. | ||
|
|
||
| An OTLP compatible endpoint should be listening for traces, metrics and logs on | ||
| `http://localhost:4317`. If not, update `OTEL_EXPORTER_OTLP_ENDPOINT` as well. | ||
|
|
||
| For example, if Elastic APM server is running locally, edit `.env` like this: | ||
| ``` | ||
| OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:8200 | ||
| ``` | ||
|
|
||
| ## Run with Docker | ||
|
|
||
| ```bash | ||
| docker compose run --build --rm genai-function-calling | ||
| ``` | ||
|
|
||
| ## Run with Python | ||
|
|
||
| First, set up a Python virtual environment like this: | ||
| ```bash | ||
| python3 -m venv .venv | ||
| source .venv/bin/activate | ||
| pip install --upgrade pip | ||
| pip install 'python-dotenv[cli]' | ||
| ``` | ||
|
|
||
| Next, install required packages: | ||
| ```bash | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| Now, use EDOT to bootstrap instrumentation (this only needs to happen once): | ||
| ```bash | ||
| edot-bootstrap --action=install | ||
| # TODO(EDOT): remove when > v0.6.1 | ||
| pip uninstall -y elastic-opentelemetry-instrumentation-openai | ||
| pip install 'elastic-opentelemetry-instrumentation-openai @ git+https://github.com/elastic/elastic-otel-python-instrumentations.git@main#subdirectory=instrumentation/elastic-opentelemetry-instrumentation-openai' | ||
| ``` | ||
|
|
||
| Finally, run `main.py` (notice the prefix of `opentelemetry-instrument): | ||
| ```bash | ||
| dotenv run --no-override -- opentelemetry-instrument python main.py | ||
| ``` | ||
|
|
||
| ## Tests | ||
|
|
||
| Tests use [pytest-vcr][pytest-vcr] to capture HTTP traffic for offline unit | ||
| testing. Recorded responses keeps test passing considering LLMs are | ||
| non-deterministic and the Elasticsearch version list changes frequently. | ||
|
|
||
| Run like this: | ||
| ```bash | ||
| pip install -r requirements-dev.txt | ||
| pytest | ||
| ``` | ||
|
|
||
| OpenAI responses routinely change as they add features, and some may cause | ||
| failures. To re-record, delete [cassettes/test_main.yaml][test_main.yaml], and | ||
| run pytest with dotenv, so that ENV variables are present: | ||
|
|
||
| ```bash | ||
| rm cassettes/test_main.yaml | ||
| dotenv -f ../.env run -- pytest | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| The LLM should generate something like "The latest stable version of | ||
| Elasticsearch is 8.17.3", unless it hallucinates. Just run it again, if you | ||
| see something else. | ||
|
|
||
| OpenAI Agents SDK's OpenTelemetry instrumentation is via | ||
| [OpenInference][openinference] and only produces traces (not logs or metrics). | ||
|
|
||
| --- | ||
| [flow]: ../README.md#example-application-flow | ||
| [openai-agents-python]: https://github.com/openai/openai-agents-python | ||
| [pytest-vcr]: https://pytest-vcr.readthedocs.io/ | ||
| [test_main.yaml]: cassettes/test_main.yaml | ||
| [openinference]: https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-openai-agents |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.