Skip to content

Commit

Permalink
feat: Add tracing support for AI responses with traceId and traceName
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Jan 16, 2025
1 parent cedc8bc commit 74d2962
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/graphql/mutations/CreateAIReply.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,18 @@ export async function createNewAIReply({
...completionOptions,
};

const { update: updateAIResponse } = createAIResponse({
const { update: updateAIResponse, getAIResponseId } = createAIResponse({
user,
docId: article.id,
type: 'AI_REPLY',
request: JSON.stringify(completionRequest),
});

// Resolves to completed or errored AI response.
const apiResult = await getOpenAI()
const apiResult = await getOpenAI({
traceId: await getAIResponseId(),
traceName: `AI Reply for article ${article.id}`,
})
.chat.completions.create(completionRequest)
.catch((error) => {
console.error(error);
Expand Down
7 changes: 5 additions & 2 deletions src/graphql/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ function extractTextFromFullTextAnnotation(fullTextAnnotation) {
export async function createTranscript(queryInfo, fileUrl, user) {
if (!user) throw new Error('[createTranscript] user is required');

const { update } = createAIResponse({
const { update, getAIResponseId } = createAIResponse({
user,
type: 'TRANSCRIPT',
docId: queryInfo.id,
Expand Down Expand Up @@ -823,7 +823,10 @@ export async function createTranscript(queryInfo, fileUrl, user) {
// Ref: https://github.com/openai/openai-node/issues/77#issuecomment-1500899486
const audio = ffmpeg(fileResp.body).noVideo().format('mp3').pipe();

const data = await getOpenAI().audio.transcriptions.create({
const data = await getOpenAI({
traceId: await getAIResponseId(),
traceName: `Whisper transcript for media ${queryInfo.id}`,
}).audio.transcriptions.create({
// Ref: https://github.com/openai/openai-node/issues/77#issuecomment-2265072410
file: await toFile(audio, 'file.mp3', { type: 'audio/mp3' }),
model: 'whisper-1',
Expand Down

0 comments on commit 74d2962

Please sign in to comment.