mirror of
https://github.com/enricoros/big-AGI.git
synced 2026-05-10 21:50:14 -07:00
Aix: unified chat-generate context
This commit is contained in:
@@ -4,7 +4,7 @@ import { getLLMIdOrThrow } from '~/common/stores/llms/store-llms';
|
||||
|
||||
import type { AixAPIChatGenerate_Request } from '~/modules/aix/server/api/aix.wiretypes';
|
||||
import { aixCGR_FromDMessages, aixCGR_SystemMessage } from '~/modules/aix/client/aix.client.chatGenerateRequest';
|
||||
import { aixChatGenerateContent_DMessage, aixCreateChatGenerateStreamContext } from '~/modules/aix/client/aix.client';
|
||||
import { aixChatGenerateContent_DMessage, aixCreateChatGenerateContext } from '~/modules/aix/client/aix.client';
|
||||
import { aixFunctionCallTool, aixRequireSingleFunctionCallInvocation } from '~/modules/aix/client/aix.client.fromSimpleFunction';
|
||||
|
||||
import { createTextContentFragment, DMessageAttachmentFragment, isImageRefPart } from '~/common/stores/chat/chat.fragments';
|
||||
@@ -69,7 +69,7 @@ Analyze the provided content to determine its nature, identify any relationships
|
||||
const { fragments } = await aixChatGenerateContent_DMessage(
|
||||
llmId,
|
||||
aixChatGenerate,
|
||||
aixCreateChatGenerateStreamContext('DEV', 'DEV'),
|
||||
aixCreateChatGenerateContext('chat-attachment-prompts', attachmentFragments[0].fId),
|
||||
false,
|
||||
{ abortSignal },
|
||||
);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z, type ZodObject } from 'zod';
|
||||
import type { ZodObject } from 'zod';
|
||||
|
||||
import type { AixAPIChatGenerate_Request } from '~/modules/aix/server/api/aix.wiretypes';
|
||||
import { aixChatGenerateContent_DMessage, aixCreateChatGenerateStreamContext } from '~/modules/aix/client/aix.client';
|
||||
import { aixChatGenerateContent_DMessage, aixCreateChatGenerateContext } from '~/modules/aix/client/aix.client';
|
||||
import { aixCGR_FromSimpleText } from '~/modules/aix/client/aix.client.chatGenerateRequest';
|
||||
import { aixFunctionCallTool, aixRequireSingleFunctionCallInvocation } from '~/modules/aix/client/aix.client.fromSimpleFunction';
|
||||
|
||||
@@ -20,9 +20,7 @@ interface CodeFix {
|
||||
outputSchema: ZodObject<any>;
|
||||
}
|
||||
|
||||
const CodeFixes: Record<string, CodeFix> = {
|
||||
|
||||
};
|
||||
const CodeFixes: Record<string, CodeFix> = {};
|
||||
|
||||
|
||||
/**
|
||||
@@ -57,7 +55,7 @@ export async function agiFixupCode(issueType: CodeFixType, codeToFix: string, er
|
||||
}),
|
||||
],
|
||||
toolsPolicy:
|
||||
config.functionPolicy === 'invoke' ? { type: 'function_call', function_call: { name: config.functionName } }
|
||||
config.functionPolicy === 'invoke' ? { type: 'function_call', function_call: { name: config.functionName } }
|
||||
: config.functionPolicy === 'think-then-invoke' ? { type: 'auto' } : undefined,
|
||||
};
|
||||
|
||||
@@ -65,7 +63,7 @@ export async function agiFixupCode(issueType: CodeFixType, codeToFix: string, er
|
||||
const { fragments } = await aixChatGenerateContent_DMessage(
|
||||
llmId,
|
||||
aixRequest,
|
||||
aixCreateChatGenerateStreamContext('DEV', 'DEV'),
|
||||
aixCreateChatGenerateContext('fixup-code', '_DEV_'),
|
||||
false,
|
||||
{ abortSignal, llmOptionsOverride: { llmTemperature: 0 /* chill the model for fixing code, we need valid json, not creativity */ } },
|
||||
);
|
||||
|
||||
@@ -3,7 +3,7 @@ import { z } from 'zod';
|
||||
import type { AixAPIChatGenerate_Request } from '~/modules/aix/server/api/aix.wiretypes';
|
||||
import { AixClientFunctionCallToolDefinition, aixFunctionCallTool, aixRequireSingleFunctionCallInvocation } from '~/modules/aix/client/aix.client.fromSimpleFunction';
|
||||
import { aixCGR_FromDMessages, aixCGR_SystemMessage } from '~/modules/aix/client/aix.client.chatGenerateRequest';
|
||||
import { aixChatGenerateContent_DMessage, aixCreateChatGenerateNSContext } from '~/modules/aix/client/aix.client';
|
||||
import { aixChatGenerateContent_DMessage, aixCreateChatGenerateContext } from '~/modules/aix/client/aix.client';
|
||||
|
||||
import { ConversationsManager } from '~/common/chat-overlay/ConversationsManager';
|
||||
import { createDMessageTextContent, messageFragmentsReduceText } from '~/common/stores/chat/chat.message';
|
||||
@@ -190,7 +190,7 @@ export async function autoChatFollowUps(conversationId: string, assistantMessage
|
||||
aixChatGenerateContent_DMessage(
|
||||
llmId,
|
||||
{ systemMessage, chatSequence, tools: [aixFunctionCallTool(diagramsTool.fun)], toolsPolicy: { type: 'any' } },
|
||||
aixCreateChatGenerateNSContext('chat-followup-diagram', conversationId),
|
||||
aixCreateChatGenerateContext('chat-followup-diagram', conversationId),
|
||||
false,
|
||||
{ abortSignal: 'NON_ABORTABLE' },
|
||||
).then(({ fragments }) => {
|
||||
@@ -241,7 +241,7 @@ export async function autoChatFollowUps(conversationId: string, assistantMessage
|
||||
aixChatGenerateContent_DMessage(
|
||||
llmId,
|
||||
{ systemMessage, chatSequence, tools: [aixFunctionCallTool(uiTool.fun)], toolsPolicy: { type: 'any' } },
|
||||
aixCreateChatGenerateNSContext('chat-followup-htmlui', conversationId),
|
||||
aixCreateChatGenerateContext('chat-followup-htmlui', conversationId),
|
||||
false,
|
||||
{ abortSignal: 'NON_ABORTABLE' },
|
||||
).then(({ fragments }) => {
|
||||
|
||||
@@ -26,9 +26,9 @@ export type AixChatGenerate_TextMessages = {
|
||||
text: string;
|
||||
}[];
|
||||
|
||||
export function aixCGR_FromSimpleText(systemMessage: string, messages: AixChatGenerate_TextMessages): AixAPIChatGenerate_Request {
|
||||
export function aixCGR_FromSimpleText(systemInstruction: string, messages: AixChatGenerate_TextMessages): AixAPIChatGenerate_Request {
|
||||
return {
|
||||
systemMessage: aixCGR_SystemMessage(systemMessage),
|
||||
systemMessage: aixCGR_SystemMessage(systemInstruction),
|
||||
chatSequence: messages.map(m => {
|
||||
switch (m.role) {
|
||||
case 'user':
|
||||
|
||||
@@ -11,7 +11,7 @@ import { metricsStoreAddChatGenerate } from '~/common/stores/metrics/store-metri
|
||||
import { presentErrorToHumans } from '~/common/util/errorUtils';
|
||||
|
||||
// NOTE: pay particular attention to the "import type", as this is importing from the server-side Zod definitions
|
||||
import type { AixAPI_Access, AixAPI_Context, AixAPI_Context_ChatGenerateNS, AixAPI_Context_ChatGenerateStream, AixAPI_Model, AixAPIChatGenerate_Request } from '../server/api/aix.wiretypes';
|
||||
import type { AixAPI_Access, AixAPI_Context_ChatGenerate, AixAPI_Model, AixAPIChatGenerate_Request } from '../server/api/aix.wiretypes';
|
||||
|
||||
import { aixCGR_FromDMessages, aixCGR_FromSimpleText, AixChatGenerate_TextMessages, clientHotFixGenerateRequestForO1Preview } from './aix.client.chatGenerateRequest';
|
||||
import { ContentReassembler } from './ContentReassembler';
|
||||
@@ -23,14 +23,10 @@ export const DEBUG_PARTICLES = false;
|
||||
const AIX_CLIENT_DEV_ASSERTS = process.env.NODE_ENV === 'development';
|
||||
|
||||
|
||||
export function aixCreateChatGenerateNSContext(name: AixAPI_Context_ChatGenerateNS['name'], ref: string): AixAPI_Context_ChatGenerateNS {
|
||||
export function aixCreateChatGenerateContext(name: AixAPI_Context_ChatGenerate['name'], ref: string | '_DEV_'): AixAPI_Context_ChatGenerate {
|
||||
return { method: 'chat-generate', name, ref };
|
||||
}
|
||||
|
||||
export function aixCreateChatGenerateStreamContext(name: AixAPI_Context_ChatGenerateStream['name'], ref: string): AixAPI_Context_ChatGenerateStream {
|
||||
return { method: 'chat-stream', name, ref };
|
||||
}
|
||||
|
||||
export function aixCreateModelFromLLMOptions(
|
||||
llmOptions: Record<string, any> | undefined,
|
||||
llmOptionsOverride: Record<string, any> | undefined,
|
||||
@@ -88,8 +84,8 @@ export async function aixChatGenerateContent_DMessage_FromHistory(
|
||||
llmId: DLLMId,
|
||||
chatHistory: Readonly<DMessage[]>,
|
||||
// aix inputs
|
||||
aixContextName: AixAPI_Context_ChatGenerateStream['name'],
|
||||
aixContextRef: AixAPI_Context['ref'],
|
||||
aixContextName: AixAPI_Context_ChatGenerate['name'],
|
||||
aixContextRef: AixAPI_Context_ChatGenerate['ref'],
|
||||
// others
|
||||
clientOptions: AixClientOptions,
|
||||
onStreamingUpdate: (update: AixChatGenerateContent_DMessage, isDone: boolean) => void,
|
||||
@@ -114,7 +110,7 @@ export async function aixChatGenerateContent_DMessage_FromHistory(
|
||||
await aixChatGenerateContent_DMessage(
|
||||
llmId,
|
||||
aixChatContentGenerateRequest,
|
||||
aixCreateChatGenerateStreamContext(aixContextName, aixContextRef),
|
||||
aixCreateChatGenerateContext(aixContextName, aixContextRef),
|
||||
true,
|
||||
clientOptions,
|
||||
(update: AixChatGenerateContent_DMessage, isDone: boolean) => {
|
||||
@@ -178,8 +174,8 @@ export async function aixChatGenerateText_Simple(
|
||||
systemInstruction: string,
|
||||
aixTextMessages: AixChatGenerate_TextMessages | string, // if string, it's a single user message - maximum simplicity
|
||||
// aix inputs
|
||||
aixContextName: AixAPI_Context_ChatGenerateNS['name'],
|
||||
aixContextRef: AixAPI_Context['ref'],
|
||||
aixContextName: AixAPI_Context_ChatGenerate['name'],
|
||||
aixContextRef: AixAPI_Context_ChatGenerate['ref'],
|
||||
// optional options
|
||||
clientOptions?: Partial<AixClientOptions>, // this makes the abortController optional
|
||||
// optional callback for streaming
|
||||
@@ -200,7 +196,7 @@ export async function aixChatGenerateText_Simple(
|
||||
);
|
||||
|
||||
// Aix Context
|
||||
const aixContext = aixCreateChatGenerateNSContext(aixContextName, aixContextRef);
|
||||
const aixContext = aixCreateChatGenerateContext(aixContextName, aixContextRef);
|
||||
|
||||
// Aix Streaming - implicit if the callback is provided
|
||||
const aixStreaming = !!onTextStreamUpdate;
|
||||
@@ -349,7 +345,7 @@ export async function aixChatGenerateContent_DMessage<TServiceSettings extends o
|
||||
llmId: DLLMId,
|
||||
// aix inputs
|
||||
aixChatGenerate: AixAPIChatGenerate_Request,
|
||||
aixContext: AixAPI_Context,
|
||||
aixContext: AixAPI_Context_ChatGenerate,
|
||||
aixStreaming: boolean,
|
||||
// others
|
||||
clientOptions: AixClientOptions,
|
||||
@@ -521,7 +517,7 @@ async function _aixChatGenerateContent_LL(
|
||||
aixAccess: AixAPI_Access,
|
||||
aixModel: AixAPI_Model,
|
||||
aixChatGenerate: AixAPIChatGenerate_Request,
|
||||
aixContext: AixAPI_Context,
|
||||
aixContext: AixAPI_Context_ChatGenerate,
|
||||
aixStreaming: boolean,
|
||||
// others
|
||||
abortSignal: AbortSignal,
|
||||
|
||||
@@ -21,7 +21,7 @@ export const aixRouter = createTRPCRouter({
|
||||
access: AixWire_API.Access_schema,
|
||||
model: AixWire_API.Model_schema,
|
||||
chatGenerate: AixWire_API_ChatContentGenerate.Request_schema,
|
||||
context: AixWire_API.Context_schema,
|
||||
context: AixWire_API.ContextChatGenerate_schema,
|
||||
streaming: z.boolean(),
|
||||
connectionOptions: AixWire_API.ConnectionOptions_schema.optional(),
|
||||
}))
|
||||
|
||||
@@ -32,9 +32,7 @@ export type AixTools_FunctionCallDefinition = Extract<z.infer<typeof AixWire_Too
|
||||
export type AixTools_ToolsPolicy = z.infer<typeof AixWire_Tooling.ToolsPolicy_schema>;
|
||||
|
||||
export type AixAPI_Access = z.infer<typeof AixWire_API.Access_schema>;
|
||||
export type AixAPI_Context = z.infer<typeof AixWire_API.Context_schema>;
|
||||
export type AixAPI_Context_ChatGenerateNS = z.infer<typeof AixWire_API.ContextChatGenerateNS_schema>;
|
||||
export type AixAPI_Context_ChatGenerateStream = z.infer<typeof AixWire_API.ContextChatGenerateStream_schema>;
|
||||
export type AixAPI_Context_ChatGenerate = z.infer<typeof AixWire_API.ContextChatGenerate_schema>;
|
||||
export type AixAPI_Model = z.infer<typeof AixWire_API.Model_schema>;
|
||||
export type AixAPIChatGenerate_Request = z.infer<typeof AixWire_API_ChatContentGenerate.Request_schema>;
|
||||
|
||||
@@ -376,32 +374,40 @@ export namespace AixWire_API {
|
||||
|
||||
/// Context
|
||||
|
||||
export const ContextChatGenerateNS_schema = z.object({
|
||||
export const ContextChatGenerate_schema = z.object({
|
||||
method: z.literal('chat-generate'),
|
||||
name: z.enum(['chat-ai-title', 'chat-ai-summarize', 'chat-followup-diagram', 'chat-followup-htmlui', 'chat-react-turn', 'draw-expand-prompt']),
|
||||
ref: z.string(),
|
||||
});
|
||||
|
||||
export const ContextChatGenerateStream_schema = z.object({
|
||||
method: z.literal('chat-stream'),
|
||||
name: z.enum([
|
||||
'DEV',
|
||||
'conversation', // chatting with a persona - conversationId
|
||||
'ai-diagram', // making a diagram - messageId
|
||||
'ai-flattener', // flattening a thread - messageId of the first message
|
||||
'aifn-gen-cr-diffs', // generating change request diffs - messageId of the change request
|
||||
'beam-gather', // fusing beam rays - fusionId
|
||||
'beam-scatter', // scattering beam rays - rayId
|
||||
'call', // having a phone conversation - messageId of the first message
|
||||
'persona-extract', // extracting a persona from texts - chainId
|
||||
|
||||
// non-streaming AI operations
|
||||
'chat-ai-summarize',
|
||||
'chat-ai-title',
|
||||
'chat-attachment-prompts', // - id of the first fragment
|
||||
'chat-followup-diagram',
|
||||
'chat-followup-htmlui',
|
||||
'chat-react-turn',
|
||||
'draw-expand-prompt',
|
||||
'fixup-code',
|
||||
|
||||
// streaming AI operations
|
||||
'ai-diagram', // making a diagram - messageId
|
||||
'ai-flattener', // flattening a thread - messageId of the first message
|
||||
'beam-gather', // fusing beam rays - fusionId
|
||||
'beam-scatter', // scattering beam rays - rayId
|
||||
'call', // having a phone conversation - messageId of the first message
|
||||
'conversation', // chatting with a persona - conversationId
|
||||
'persona-extract', // extracting a persona from texts - chainId
|
||||
|
||||
// temporary (nothing is more permanent than a temporary fix that works well)
|
||||
'_DEV_',
|
||||
|
||||
]),
|
||||
ref: z.string(),
|
||||
});
|
||||
|
||||
export const Context_schema = z.discriminatedUnion('method', [
|
||||
ContextChatGenerateNS_schema,
|
||||
ContextChatGenerateStream_schema,
|
||||
]);
|
||||
// For future use
|
||||
// export const Context_schema = z.discriminatedUnion('method', [
|
||||
// ContextChatGenerate_schema,
|
||||
// ]);
|
||||
|
||||
/// Connection options
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { escapeXml } from '~/server/wire';
|
||||
|
||||
import { AixAPI_Model, AixAPIChatGenerate_Request, AixMessages_ChatMessage, AixParts_MetaInReferenceToPart, AixTools_ToolDefinition, AixTools_ToolsPolicy } from '../../../api/aix.wiretypes';
|
||||
import type { AixAPI_Model, AixAPIChatGenerate_Request, AixMessages_ChatMessage, AixParts_MetaInReferenceToPart, AixTools_ToolDefinition, AixTools_ToolsPolicy } from '../../../api/aix.wiretypes';
|
||||
import { AnthropicWire_API_Message_Create, AnthropicWire_Blocks } from '../../wiretypes/anthropic.wiretypes';
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { OpenAIDialects } from '~/modules/llms/server/openai/openai.router';
|
||||
|
||||
import { AixAPI_Model, AixAPIChatGenerate_Request, AixMessages_ChatMessage, AixMessages_SystemMessage, AixParts_MetaInReferenceToPart, AixTools_ToolDefinition, AixTools_ToolsPolicy } from '../../../api/aix.wiretypes';
|
||||
import type { AixAPI_Model, AixAPIChatGenerate_Request, AixMessages_ChatMessage, AixMessages_SystemMessage, AixParts_MetaInReferenceToPart, AixTools_ToolDefinition, AixTools_ToolsPolicy } from '../../../api/aix.wiretypes';
|
||||
import { OpenAIWire_API_Chat_Completions, OpenAIWire_ContentParts, OpenAIWire_Messages } from '../../wiretypes/openai.wiretypes';
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user