-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into add-github-login
- Loading branch information
Showing
37 changed files
with
1,834 additions
and
1,287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
app/(playground)/p/[agentId]/beta-proto/artifact/server-actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
"use server"; | ||
|
||
import { openai } from "@ai-sdk/openai"; | ||
import { streamObject } from "ai"; | ||
import { createStreamableValue } from "ai/rsc"; | ||
|
||
import { getUserSubscriptionId, isRoute06User } from "@/app/(auth)/lib"; | ||
import { metrics } from "@opentelemetry/api"; | ||
import { Langfuse } from "langfuse"; | ||
import { schema as artifactSchema } from "../artifact/schema"; | ||
import type { SourceIndex } from "../source/types"; | ||
import { sourceIndexesToSources, sourcesToText } from "../source/utils"; | ||
import type { AgentId } from "../types"; | ||
|
||
type GenerateArtifactStreamParams = { | ||
agentId: AgentId; | ||
userPrompt: string; | ||
sourceIndexes: SourceIndex[]; | ||
}; | ||
export async function generateArtifactStream( | ||
params: GenerateArtifactStreamParams, | ||
) { | ||
const lf = new Langfuse(); | ||
const trace = lf.trace({ | ||
id: `giselle-${Date.now()}`, | ||
}); | ||
const sources = await sourceIndexesToSources({ | ||
input: { | ||
agentId: params.agentId, | ||
sourceIndexes: params.sourceIndexes, | ||
}, | ||
}); | ||
|
||
const system = | ||
sources.length > 0 | ||
? ` | ||
Your primary objective is to fulfill the user's request by utilizing the information provided within the <Source> or <WebPage> tags. Analyze the structured content carefully and leverage it to generate accurate and relevant responses. Focus on addressing the user's needs effectively while maintaining coherence and context throughout the interaction. | ||
If you use the information provided in the <WebPage>, After each piece of information, add a superscript number for citation (e.g. 1, 2, etc.). | ||
${sourcesToText(sources)} | ||
` | ||
: "You generate an answer to a question. "; | ||
|
||
const stream = createStreamableValue(); | ||
|
||
(async () => { | ||
const model = "gpt-4o"; | ||
const generation = trace.generation({ | ||
input: params.userPrompt, | ||
model, | ||
}); | ||
const { partialObjectStream, object } = await streamObject({ | ||
model: openai(model), | ||
system, | ||
prompt: params.userPrompt, | ||
schema: artifactSchema, | ||
onFinish: async (result) => { | ||
const meter = metrics.getMeter("OpenAI"); | ||
const tokenCounter = meter.createCounter("token_consumed", { | ||
description: "Number of OpenAI API tokens consumed by each request", | ||
}); | ||
const subscriptionId = await getUserSubscriptionId(); | ||
const isR06User = await isRoute06User(); | ||
tokenCounter.add(result.usage.totalTokens, { | ||
subscriptionId, | ||
isR06User, | ||
}); | ||
generation.end({ | ||
output: result, | ||
}); | ||
await lf.shutdownAsync(); | ||
}, | ||
}); | ||
|
||
for await (const partialObject of partialObjectStream) { | ||
stream.update(partialObject); | ||
} | ||
|
||
const result = await object; | ||
|
||
stream.done(); | ||
})(); | ||
|
||
return { object: stream.value }; | ||
} |
42 changes: 42 additions & 0 deletions
42
app/(playground)/p/[agentId]/beta-proto/connector/actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { ConnectorObject } from "./types"; | ||
|
||
const v2ConnectorActionTypes = { | ||
set: "v2.setConnectors", | ||
} as const; | ||
type V2ConnectorActionType = | ||
(typeof v2ConnectorActionTypes)[keyof typeof v2ConnectorActionTypes]; | ||
|
||
interface SetConnectorsAction { | ||
type: Extract<V2ConnectorActionType, "v2.setConnectors">; | ||
input: SetConnectorsInput; | ||
} | ||
interface SetConnectorsInput { | ||
connectors: ConnectorObject[]; | ||
} | ||
export function setConnectors({ | ||
input, | ||
}: { input: SetConnectorsInput }): SetConnectorsAction { | ||
return { | ||
type: v2ConnectorActionTypes.set, | ||
input, | ||
}; | ||
} | ||
export type V2ConnectorAction = SetConnectorsAction; | ||
export function isV2ConnectorAction( | ||
action: unknown, | ||
): action is V2ConnectorAction { | ||
return Object.values(v2ConnectorActionTypes).includes( | ||
(action as V2ConnectorAction).type, | ||
); | ||
} | ||
|
||
export function v2ConnectorReducer( | ||
connectors: ConnectorObject[], | ||
action: V2ConnectorAction, | ||
): ConnectorObject[] { | ||
switch (action.type) { | ||
case v2ConnectorActionTypes.set: | ||
return action.input.connectors; | ||
} | ||
return connectors; | ||
} |
31 changes: 31 additions & 0 deletions
31
app/(playground)/p/[agentId]/beta-proto/connector/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { GiselleNodeArchetype } from "../giselle-node/blueprints"; | ||
import type { GiselleNodeCategory, GiselleNodeId } from "../giselle-node/types"; | ||
import { createConnectorId } from "./factory"; | ||
import type { ConnectorObject } from "./types"; | ||
|
||
interface BuildConnectorArgs { | ||
sourceNode: { | ||
id: GiselleNodeId; | ||
category: GiselleNodeCategory; | ||
archetype: GiselleNodeArchetype; | ||
}; | ||
targetNode: { | ||
id: GiselleNodeId; | ||
handle: string; | ||
category: GiselleNodeCategory; | ||
archetype: GiselleNodeArchetype; | ||
}; | ||
} | ||
export function buildConnector(args: BuildConnectorArgs) { | ||
return { | ||
id: createConnectorId(), | ||
object: "connector", | ||
source: args.sourceNode.id, | ||
sourceNodeCategory: args.sourceNode.category, | ||
sourceNodeArcheType: args.sourceNode.archetype, | ||
target: args.targetNode.id, | ||
targetHandle: args.targetNode.handle, | ||
targetNodeCategory: args.targetNode.category, | ||
targetNodeArcheType: args.targetNode.archetype, | ||
} satisfies ConnectorObject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.