Skip to content

add option to call Promptflow Endpoint #283

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion src/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ AZURE_SPEECH_REGION=
AZURE_SPEECH_KEY=

# Enabled must be set to "true" any other value will disable the feature
PUBLIC_SPEECH_ENABLED=true
PUBLIC_SPEECH_ENABLED=true

# Promptflow API
PROMPT_FLOW_API_URL=
PROMPT_FLOW_API_KEY=
3 changes: 3 additions & 0 deletions src/features/chat/chat-services/chat-api-entry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChatAPIData } from "./chat-api-data";
import { ChatAPISimple } from "./chat-api-simple";
import { ChatAPIPromptFlow } from "./chat-api-pf"
import { PromptGPTProps } from "./models";

export const chatAPIEntry = async (props: PromptGPTProps) => {
Expand All @@ -9,6 +10,8 @@ export const chatAPIEntry = async (props: PromptGPTProps) => {
return await ChatAPIData(props);
} else if (props.chatType === "mssql") {
return await ChatAPIData(props);
} else if (props.chatType === "promptflow") {
return await ChatAPIPromptFlow(props);
} else {
return await ChatAPISimple(props);
}
Expand Down
77 changes: 77 additions & 0 deletions src/features/chat/chat-services/chat-api-pf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { userHashedId } from "@/features/auth/helpers";
import { StreamingTextResponse } from "ai";
import { initAndGuardChatSession } from "./chat-thread-service";
import { CosmosDBChatMessageHistory } from "./cosmosdb/cosmosdb";
import { PromptGPTProps, PromptflowChatMessage } from "./models";
import axios from "axios";
import { ChatCompletionMessage } from "openai/resources";

export const ChatAPIPromptFlow = async (props: PromptGPTProps) => {
const { lastHumanMessage, chatThread } = await initAndGuardChatSession(props);
const userId = await userHashedId();

const chatHistory = new CosmosDBChatMessageHistory({
sessionId: chatThread.id,
userId: userId,
});

const history = await chatHistory.getMessages();
const topHistory = history.slice(history.length - 30, history.length);

try {
const pfHistory = topHistory.reduce(
(acc: PromptflowChatMessage[], message: ChatCompletionMessage) => {
if (message.role === "user") {
acc.push({
inputs: {
question: message.content || "",
},
outputs: {},
});
} else if (message.role === "assistant" && acc.length > 0) {
acc[acc.length - 1].outputs.answer = message.content || "";
}
return acc;
},
[]
);

const response = await axios.post(
`${process.env.PROMPT_FLOW_API_URL}`,
{
question: lastHumanMessage.content,
chat_history: pfHistory,
},
{
headers: {
Authorization: `Bearer ${process.env.PROMPT_FLOW_API_KEY}`,
},
}
);


await chatHistory.addMessage({
content: lastHumanMessage.content,
role: "user",
});

await chatHistory.addMessage({
content: response.data.answer,
role: "assistant",
});

return new StreamingTextResponse(response.data.answer);
} catch (e: unknown) {
if (e instanceof Error) {
return new Response(e.message, {
status: 500,
statusText: e.toString(),
});
} else {
return new Response("An unknown error occurred.", {
status: 500,
statusText: "Unknown Error",
});
}
}
};
15 changes: 14 additions & 1 deletion src/features/chat/chat-services/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface ChatMessageModel {
}

export type ConversationStyle = "creative" | "balanced" | "precise";
export type ChatType = "simple" | "data" | "mssql";
export type ChatType = "simple" | "data" | "mssql" | "promptflow";

export type ChatRole = "system" | "user" | "assistant" | "function";

Expand Down Expand Up @@ -60,3 +60,16 @@ export interface ServerActionResponse<T> {
error: string;
response: T;
}

export interface PromptflowChatMessageMessageInput {
question: string;
}

export interface PromptflowChatMessageMessageOutput {
answer?: string;
}

export interface PromptflowChatMessage {
inputs: PromptflowChatMessageMessageInput;
outputs: PromptflowChatMessageMessageOutput;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { FileText, MessageCircle } from "lucide-react";
import { FileText, MessageCircle, Globe } from "lucide-react";
import { FC } from "react";
import { ChatType } from "../../chat-services/models";
import { useChatContext } from "../chat-context";
Expand All @@ -16,7 +16,7 @@ export const ChatTypeSelector: FC<Prop> = (props) => {
defaultValue={chatBody.chatType}
onValueChange={(value) => onChatTypeChange(value as ChatType)}
>
<TabsList className="grid w-full grid-cols-2 h-12 items-stretch">
<TabsList className="grid w-full grid-cols-3 h-12 items-stretch">
<TabsTrigger
value="simple"
className="flex gap-2"
Expand All @@ -31,6 +31,13 @@ export const ChatTypeSelector: FC<Prop> = (props) => {
>
<FileText size={20} /> File
</TabsTrigger>
<TabsTrigger
value="promptflow"
className="flex gap-2"
disabled={props.disable}
>
<Globe size={20} /> PF API
</TabsTrigger>
</TabsList>
</Tabs>
);
Expand Down
39 changes: 37 additions & 2 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@types/react-dom": "^18.2.14",
"ai": "^2.2.20",
"autoprefixer": "^10.4.16",
"axios": "^1.6.4",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"eslint": "^8.52.0",
Expand Down
2 changes: 2 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const azureEnvVars = [
"AZURE_SPEECH_REGION",
"AZURE_SPEECH_KEY",
"PUBLIC_PUBLIC_SPEECH_ENABLED",
"PROMPT_FLOW_API_URL",
"PROMPT_FLOW_API_KEY",
] as const;

type RequiredServerEnvKeys = (typeof azureEnvVars)[number];
Expand Down