Files
ai-inference/README.md

179 lines
7.8 KiB
Markdown
Raw Normal View History

2025-04-07 00:09:42 +00:00
# AI Inference in GitHub Actions
2025-04-04 07:27:58 +11:00
[![GitHub Super-Linter](https://github.com/actions/typescript-action/actions/workflows/linter.yml/badge.svg)](https://github.com/super-linter/super-linter)
![CI](https://github.com/actions/typescript-action/actions/workflows/ci.yml/badge.svg)
[![Check dist/](https://github.com/actions/typescript-action/actions/workflows/check-dist.yml/badge.svg)](https://github.com/actions/typescript-action/actions/workflows/check-dist.yml)
[![CodeQL](https://github.com/actions/typescript-action/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/actions/typescript-action/actions/workflows/codeql-analysis.yml)
[![Coverage](./badges/coverage.svg)](./badges/coverage.svg)
2025-04-07 00:09:42 +00:00
Use AI models from [GitHub Models](https://github.com/marketplace/models) in
2025-04-10 08:53:19 -04:00
your workflows.
2025-04-04 07:27:58 +11:00
2025-04-07 00:09:42 +00:00
## Usage
2025-04-04 07:27:58 +11:00
2025-04-07 00:09:42 +00:00
Create a workflow to use the AI inference action:
2025-04-04 07:27:58 +11:00
```yaml
2025-04-08 02:10:27 +00:00
name: 'AI inference'
2025-04-08 12:00:25 +10:00
on: workflow_dispatch
jobs:
inference:
permissions:
models: read
runs-on: ubuntu-latest
steps:
- name: Test Local Action
id: inference
uses: actions/ai-inference@v1
with:
prompt: 'Hello!'
2025-04-08 02:10:27 +00:00
2025-04-08 12:00:25 +10:00
- name: Print Output
id: output
run: echo "${{ steps.inference.outputs.response }}"
2025-04-04 07:27:58 +11:00
```
2025-04-17 20:23:07 +00:00
### Using a prompt file
You can also provide a prompt file instead of an inline prompt:
```yaml
steps:
- name: Run AI Inference with Prompt File
id: inference
uses: actions/ai-inference@v1
with:
prompt-file: './path/to/prompt.txt'
2025-04-17 20:23:07 +00:00
```
feat: Add system-prompt-file input for file-based system prompts This enhancement adds the ability to load a system prompt from a file, similar to the existing prompt-file functionality, providing more flexibility when working with complex system prompts. Key changes: - Added new `system-prompt-file` input to action.yml with proper description - Updated main.ts implementation to handle file-based system prompts with: - File existence checking and appropriate error handling - Proper precedence (system-prompt-file takes priority over system-prompt) - Consistent error messages with existing prompt-file implementation Test coverage added: - Basic usage: Test verifies system-prompt-file loads content correctly - Error handling: Test ensures proper errors when system-prompt-file doesn't exist - Precedence: Test confirms system-prompt-file overrides system-prompt when both exist - Integration: Test validates both prompt-file and system-prompt-file work together - Max tokens: Test verifies custom token limits are properly passed to the model - Testing infrastructure: Improved mock implementations that detect unexpected calls Documentation: - Updated README.md with system-prompt-file in inputs table - Added dedicated usage example for system-prompt-file - Fixed formatting in inputs table CI/CD: - Updated workflow to test system-prompt-file functionality with actual file content This feature maintains backward compatibility while adding a useful option for workflows that need to use more complex system prompts stored in files.
2025-05-24 03:50:15 +02:00
### Using a system prompt file
In addition to the regular prompt, you can provide a system prompt file instead
of an inline system prompt:
```yaml
steps:
- name: Run AI Inference with System Prompt File
id: inference
uses: actions/ai-inference@v1
with:
prompt: 'Hello!'
system-prompt-file: './path/to/system-prompt.txt'
```
2025-04-17 20:23:07 +00:00
### Read output from file instead of output
This can be useful when model response exceeds actions output limit
```yaml
steps:
- name: Test Local Action
id: inference
uses: actions/ai-inference@v1
with:
prompt: 'Hello!'
- name: Use Response File
run: |
2025-04-17 20:23:07 +00:00
echo "Response saved to: ${{ steps.inference.outputs.response-file }}"
cat "${{ steps.inference.outputs.response-file }}"
```
2025-07-15 23:23:39 +00:00
### GitHub MCP Integration (Model Context Protocol)
2025-07-16 02:19:49 +00:00
This action now supports **read-only** integration with the GitHub-hosted Model
Context Protocol (MCP) server, which provides access to GitHub tools like
repository management, issue tracking, and pull request operations.
2025-07-15 23:23:39 +00:00
```yaml
steps:
- name: AI Inference with GitHub Tools
id: inference
uses: actions/ai-inference@v1
with:
prompt: 'List my open pull requests and create a summary'
2025-07-16 02:19:49 +00:00
enable-github-mcp: true
2025-07-15 23:23:39 +00:00
```
When MCP is enabled, the AI model will have access to GitHub tools and can
2025-07-16 02:19:49 +00:00
perform actions like searching issues and PRs.
2025-07-15 23:23:39 +00:00
2025-07-16 02:19:49 +00:00
**Note:** MCP integration requires your workflow token to have appropriate
GitHub permissions for the operations the AI will perform.
2025-07-15 23:23:39 +00:00
2025-04-07 00:09:42 +00:00
## Inputs
2025-04-04 07:27:58 +11:00
2025-04-07 00:09:42 +00:00
Various inputs are defined in [`action.yml`](action.yml) to let you configure
the action:
2025-04-04 07:27:58 +11:00
2025-07-16 02:19:49 +00:00
| Name | Description | Default |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ |
| `token` | Token to use for inference. Typically the GITHUB_TOKEN secret | `github.token` |
| `prompt` | The prompt to send to the model | N/A |
| `prompt-file` | Path to a file containing the prompt. If both `prompt` and `prompt-file` are provided, `prompt-file` takes precedence | `""` |
| `system-prompt` | The system prompt to send to the model | `"You are a helpful assistant"` |
| `system-prompt-file` | Path to a file containing the system prompt. If both `system-prompt` and `system-prompt-file` are provided, `system-prompt-file` takes precedence | `""` |
2025-06-20 00:21:26 +00:00
| `model` | The model to use for inference. Must be available in the [GitHub Models](https://github.com/marketplace?type=models) catalog | `openai/gpt-4.1` |
2025-07-16 02:19:49 +00:00
| `endpoint` | The endpoint to use for inference. If you're running this as part of an org, you should probably use the org-specific Models endpoint | `https://models.github.ai/inference` |
| `max-tokens` | The max number of tokens to generate | 200 |
| `enable-github-mcp` | Enable Model Context Protocol integration with GitHub tools | `false` |
2025-04-04 07:27:58 +11:00
2025-04-07 00:09:42 +00:00
## Outputs
2025-04-04 07:27:58 +11:00
2025-04-07 00:09:42 +00:00
The AI inference action provides the following outputs:
2025-04-04 07:27:58 +11:00
| Name | Description |
| --------------- | ----------------------------------------------------------------------- |
| `response` | The response from the model |
2025-04-17 20:23:07 +00:00
| `response-file` | The file path where the response is saved (useful for larger responses) |
2025-04-04 07:27:58 +11:00
2025-04-07 00:09:42 +00:00
## Required Permissions
In order to run inference with GitHub Models, the GitHub AI inference action
requires `models` permissions.
```yml
permissions:
contents: read
models: read
2025-04-04 07:27:58 +11:00
```
## Publishing a New Release
This project includes a helper script, [`script/release`](./script/release)
designed to streamline the process of tagging and pushing new releases for
2025-04-07 00:09:42 +00:00
GitHub Actions. For more information, see
[Versioning](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)
in the GitHub Actions toolkit.
2025-04-04 07:27:58 +11:00
GitHub Actions allows users to select a specific version of the action to use,
based on release tags. This script simplifies this process by performing the
following steps:
1. **Retrieving the latest release tag:** The script starts by fetching the most
recent SemVer release tag of the current branch, by looking at the local data
available in your repository.
1. **Prompting for a new release tag:** The user is then prompted to enter a new
release tag. To assist with this, the script displays the tag retrieved in
the previous step, and validates the format of the inputted tag (vX.X.X). The
user is also reminded to update the version field in package.json.
1. **Tagging the new release:** The script then tags a new release and syncs the
separate major tag (e.g. v1, v2) with the new release tag (e.g. v1.0.0,
v2.1.2). When the user is creating a new major release, the script
auto-detects this and creates a `releases/v#` branch for the previous major
version.
1. **Pushing changes to remote:** Finally, the script pushes the necessary
commits, tags and branches to the remote repository. From here, you will need
to create a new release in GitHub so users can easily reference the new tags
in their workflows.
2025-04-10 21:33:06 +00:00
## License
2025-04-10 21:24:37 +00:00
2025-04-10 21:33:06 +00:00
This project is licensed under the terms of the MIT open source license. Please
refer to [MIT](./LICENSE.txt) for the full terms.
2025-04-10 21:24:37 +00:00
2025-04-07 00:09:42 +00:00
## Contributions
2025-04-04 07:27:58 +11:00
2025-04-07 00:09:42 +00:00
Contributions are welcome! See the [Contributor's Guide](CONTRIBUTING.md).