Skip to content
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

feat: firefall conversation support (WIP) #285

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 70 additions & 39 deletions packages/spacecat-shared-gpt-client/src/clients/firefall-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function validateFirefallResponse(response) {
|| !hasText(response.generations[0][0].text));
}

function templatePrompt(prompt, context) {
return prompt.replace(/\{\{(\w+)\}\}/g, (_, key) => context[key] || '');
}

export default class FirefallClient {
static createFrom(context) {
const { log = console } = context;
Expand Down Expand Up @@ -71,6 +75,7 @@ export default class FirefallClient {
*/
constructor(config, log) {
this.config = config;
this.apiBaseUrl = `${config.apiEndpoint}/v2/`;
this.log = log;
this.imsClient = config.imsClient;
this.apiAuth = null;
Expand All @@ -89,69 +94,57 @@ export default class FirefallClient {
this.log.debug(`${message}: took ${duration}ms`);
}

async #submitJob(prompt) {
async #apiCall(path, method, body = null) {
const apiAuth = await this.#getApiAuth();

const body = JSON.stringify({
input: prompt,
capability_name: this.config.capabilityName,
});

const url = createUrl(`${this.config.apiEndpoint}/v2/capability_execution/job`);
const url = `${this.apiBaseUrl}${path}`;
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiAuth}`,
'x-api-key': this.config.apiKey,
'x-gw-ims-org-id': this.config.imsOrg,
};

const options = {
method,
headers,
};

if (body) {
options.body = JSON.stringify(body);
}

this.log.info(`URL: ${url}, Headers: ${JSON.stringify(headers)}`);

const response = await httpFetch(url, {
method: 'POST',
headers,
body,
});
const response = await httpFetch(createUrl(url), options);

if (!response.ok) {
throw new Error(`Job submission failed with status code ${response.status}`);
const msg = await response.text();
throw new Error(`API call failed with status code ${response.status}: ${msg}`);
}

return response.json();
}

async #submitJob(prompt) {
const path = 'capability_execution/job';
const body = {
input: prompt,
capability_name: this.config.capabilityName,
};

return this.#apiCall(path, 'POST', body);
}

/* eslint-disable no-await-in-loop */
async #pollJobStatus(jobId) {
const apiAuth = await this.#getApiAuth();

let jobStatusResponse;
do {
await new Promise(
(resolve) => { setTimeout(resolve, this.config.pollInterval); },
); // Wait for 2 seconds before polling

const url = `${this.config.apiEndpoint}/v2/capability_execution/job/${jobId}`;
const headers = {
Authorization: `Bearer ${apiAuth}`,
'x-api-key': this.config.apiKey,
'x-gw-ims-org-id': this.config.imsOrg,
};

this.log.info(`URL: ${url}, Headers: ${JSON.stringify(headers)}`);

const response = await httpFetch(
createUrl(url),
{
method: 'GET',
headers,
},
);

if (!response.ok) {
throw new Error(`Job polling failed with status code ${response.status}`);
}
); // Wait for pollInterval before polling

jobStatusResponse = await response.json();
const url = `capability_execution/job/${jobId}`;
jobStatusResponse = await this.#apiCall(url, 'GET');
} while (jobStatusResponse.status === 'PROCESSING' || jobStatusResponse.status === 'WAITING');

if (jobStatusResponse.status !== 'SUCCEEDED') {
Expand All @@ -161,6 +154,44 @@ export default class FirefallClient {
return jobStatusResponse;
}

async #createConversationSession() {
const url = 'conversation';
const body = { capability_name: this.config.capabilityName, conversation_name: 'spacecat' };

const response = await this.#apiCall(url, 'POST', body);
return response.conversation_id;
}

async #submitPromptToConversation(sessionId, prompt) {
const path = 'query';
const body = { dialogue: { question: prompt }, conversation_id: sessionId };

return this.#apiCall(path, 'POST', body);
}

async executePromptChain(chainConfig) {
const sessionId = await this.#createConversationSession();

let context = {};
for (const step of chainConfig.steps) {
const prompt = templatePrompt(step.prompt, context);
const response = await this.#submitPromptToConversation(sessionId, prompt);
const { answer } = response.dialogue;

if (step.onResponse) {
const result = step.onResponse(answer, context);
if (result.abort) {
this.log.info('Prompt chain aborted.');
break;
}
context = { ...context, ...result.context };
} else {
context = { ...context, ...response };
}
}
return context;
}

async fetch(prompt) {
if (!hasText(prompt)) {
throw new Error('Invalid prompt received');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,44 @@ describe('FirefallClient', () => {
await expect(client.fetch('Test prompt')).to.be.rejectedWith('Invalid response format.');
});
});

describe('executePromptChain', async () => {
let client;

beforeEach(() => {
client = FirefallClient.createFrom(mockContext);
});

it('executes a prompt chain successfully using conversation API', async () => {
const chainConfig = {
steps: [
{
prompt: 'What is the capital of France?',
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onResponse: (response) => ({ context: { capital: 'Paris' } }),
},
{
prompt: 'What is the population of {{capital}}?',
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onResponse: (response) => ({ context: { population: '2 million' } }),
},
],
};

nock(mockContext.env.FIREFALL_API_ENDPOINT)
.post('/v2/conversations')
.reply(200, { conversation_id: 'sessionId' })
.post('/v2/conversations/sessionId/messages')
.reply(200, { messages: [{ id: 'messageId1', content: 'Paris', status: 'SUCCEEDED' }] })
.get('/v2/conversations/sessionId/messages/messageId1')
.reply(200, { messages: [{ content: 'Paris' }], status: 'SUCCEEDED' })
.post('/v2/conversations/sessionId/messages')
.reply(200, { messages: [{ id: 'messageId2', content: '2 million', status: 'SUCCEEDED' }] })
.get('/v2/conversations/sessionId/messages/messageId2')
.reply(200, { messages: [{ content: '2 million' }], status: 'SUCCEEDED' });

const result = await client.executePromptChain(chainConfig);
expect(result).to.eql({ capital: 'Paris', population: '2 million' });
});
});
});
Loading