Setup initial inference flow and install packages
This commit is contained in:
+35
-9
@@ -1,5 +1,6 @@
|
||||
import * as core from '@actions/core'
|
||||
import { wait } from './wait.js'
|
||||
import ModelClient, { isUnexpected } from '@azure-rest/ai-inference'
|
||||
import { AzureKeyCredential } from '@azure/core-auth'
|
||||
|
||||
/**
|
||||
* The main function for the action.
|
||||
@@ -8,18 +9,43 @@ import { wait } from './wait.js'
|
||||
*/
|
||||
export async function run(): Promise<void> {
|
||||
try {
|
||||
const ms: string = core.getInput('milliseconds')
|
||||
const prompt: string = core.getInput('prompt')
|
||||
const systemPrompt: string = core.getInput('system_prompt')
|
||||
const modelName = core.getInput('model_name') || 'gpt-4o'
|
||||
|
||||
// Debug logs are only output if the `ACTIONS_STEP_DEBUG` secret is true
|
||||
core.debug(`Waiting ${ms} milliseconds ...`)
|
||||
const token = process.env['GITHUB_TOKEN']
|
||||
if (token === undefined) {
|
||||
throw new Error('GITHUB_TOKEN is not set')
|
||||
}
|
||||
const endpoint = 'https://models.inference.ai.azure.com'
|
||||
|
||||
// Log the current timestamp, wait, then log the new timestamp
|
||||
core.debug(new Date().toTimeString())
|
||||
await wait(parseInt(ms, 10))
|
||||
core.debug(new Date().toTimeString())
|
||||
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,
|
||||
max_tokens: 1000,
|
||||
model: modelName
|
||||
}
|
||||
})
|
||||
|
||||
if (isUnexpected(response)) {
|
||||
throw response.body.error
|
||||
}
|
||||
|
||||
const modelResponse: string | null =
|
||||
response.body.choices[0].message.content
|
||||
|
||||
// Set outputs for other workflow steps to use
|
||||
core.setOutput('time', new Date().toTimeString())
|
||||
core.setOutput('response', modelResponse || '')
|
||||
} catch (error) {
|
||||
// Fail the workflow run if an error occurs
|
||||
if (error instanceof Error) core.setFailed(error.message)
|
||||
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
/**
|
||||
* Waits for a number of milliseconds.
|
||||
*
|
||||
* @param milliseconds The number of milliseconds to wait.
|
||||
* @returns Resolves with 'done!' after the wait is over.
|
||||
*/
|
||||
export async function wait(milliseconds: number): Promise<string> {
|
||||
return new Promise((resolve) => {
|
||||
if (isNaN(milliseconds)) throw new Error('milliseconds is not a number')
|
||||
|
||||
setTimeout(() => resolve('done!'), milliseconds)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user