Files
ai-inference/src/main.ts

59 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-04-04 07:27:58 +11:00
import * as core from '@actions/core'
import ModelClient, { isUnexpected } from '@azure-rest/ai-inference'
import { AzureKeyCredential } from '@azure/core-auth'
2025-04-04 07:27:58 +11:00
/**
* The main function for the action.
*
* @returns Resolves when the action is complete.
*/
export async function run(): Promise<void> {
try {
const prompt: string = core.getInput('prompt')
2025-04-06 23:21:29 +00:00
if (prompt === undefined || prompt === '') {
throw new Error('prompt is not set')
}
const systemPrompt: string = core.getInput('system_prompt')
2025-04-07 00:09:42 +00:00
const modelName: string = core.getInput('model')
const maxTokens: number = parseInt(core.getInput('max_tokens'), 10)
2025-04-04 07:27:58 +11:00
const token = process.env['GITHUB_TOKEN']
if (token === undefined) {
throw new Error('GITHUB_TOKEN is not set')
}
2025-04-07 00:09:42 +00:00
const endpoint = core.getInput('endpoint')
2025-04-04 07:27:58 +11:00
const client = ModelClient(endpoint, new AzureKeyCredential(token))
const response = await client.path('/chat/completions').post({
body: {
messages: [
{
role: 'system',
content: systemPrompt || 'You are a helpful assistant.'
},
{ role: 'user', content: prompt }
],
temperature: 1.0,
top_p: 1.0,
2025-04-07 00:09:42 +00:00
max_tokens: maxTokens,
model: modelName
}
})
if (isUnexpected(response)) {
throw response.body.error
}
const modelResponse: string | null =
response.body.choices[0].message.content
2025-04-04 07:27:58 +11:00
// Set outputs for other workflow steps to use
core.setOutput('response', modelResponse || '')
2025-04-04 07:27:58 +11:00
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
}
}