Skip to content

Commit

Permalink
feat: support dify in playground & demo
Browse files Browse the repository at this point in the history
  • Loading branch information
plutoless committed Dec 17, 2024
1 parent a165146 commit d5dba93
Show file tree
Hide file tree
Showing 17 changed files with 460 additions and 188 deletions.
342 changes: 180 additions & 162 deletions agents/examples/demo/property.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
{}
{
"token": "${env:COZE_TOKEN}",
"bot_id": "${env:COZE_BOT_ID}",
"base_url": "https://api.coze.cn",
"prompt": "",
"greeting": "TEN Agent connected with Coze. How can I help you today?"
}
17 changes: 16 additions & 1 deletion agents/ten_packages/extension/dify_python/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,20 @@
"tests/**"
]
},
"api": {}
"api": {
"property": {
"user_id": {
"type": "string"
},
"api_key": {
"type": "string"
},
"base_url": {
"type": "string"
},
"greeting": {
"type": "string"
}
}
}
}
7 changes: 6 additions & 1 deletion agents/ten_packages/extension/dify_python/property.json
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
{}
{
"user_id": "User",
"api_key": "${env:DIFY_API_KEY}",
"base_url": "https://api.dify.ai/v1",
"greeting": "TEN Agent connected with Dify. How can I help you today?"
}
12 changes: 12 additions & 0 deletions demo/src/app/api/agents/start/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ export const getGraphProperties = (
// "greeting": combined_greeting,
}
}
} else if (graphName == "va_dify_azure") {
return {
"agora_rtc": {
"agora_asr_language": language,
},
"llm": {
"greeting": combined_greeting,
},
"tts": {
"azure_synthesis_voice_name": voiceNameMap[language]["azure"][voiceType]
}
}
}
return {}
}
4 changes: 4 additions & 0 deletions demo/src/app/api/agents/start/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function POST(request: NextRequest) {
coze_token,
coze_bot_id,
coze_base_url,
dify_api_key,
} = body;

let properties: any = getGraphProperties(graph_name, language, voice_type, prompt, greeting);
Expand All @@ -37,6 +38,9 @@ export async function POST(request: NextRequest) {
properties["coze_python_async"]["bot_id"] = coze_bot_id;
properties["coze_python_async"]["base_url"] = coze_base_url;
}
if (graph_name.includes("dify")) {
properties["llm"]["api_key"] = dify_api_key;
}

console.log(`Starting agent for request ID: ${JSON.stringify({
request_id,
Expand Down
18 changes: 14 additions & 4 deletions demo/src/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
VoiceOptionItem,
GraphOptionItem,
ICozeSettings,
IDifySettings,
} from "@/types"
export const GITHUB_URL = "https://github.com/TEN-framework/TEN-Agent"
export const API_GH_GET_REPO_INFO =
"https://api.github.com/repos/TEN-framework/TEN-Agent"
export const OPTIONS_KEY = "__options__"
export const AGENT_SETTINGS_KEY = "__agent_settings__"
export const COZE_SETTINGS_KEY = "__coze_settings__"
export const DIFY_SETTINGS_KEY = "__dify_settings__"
export const DEFAULT_OPTIONS: IOptions = {
channel: "",
userName: "",
Expand All @@ -36,6 +38,10 @@ export const DEFAULT_COZE_SETTINGS: ICozeSettings = {
base_url: ECozeBaseUrl.GLOBAL,
}

export const DEFAULT_DIFY_SETTINGS: IDifySettings = {
api_key: "",
}

export const DESCRIPTION = "A Realtime Conversational AI Agent powered by TEN"
export const LANGUAGE_OPTIONS: LanguageOptionItem[] = [
{
Expand All @@ -56,6 +62,14 @@ export const LANGUAGE_OPTIONS: LanguageOptionItem[] = [
},
]
export const GRAPH_OPTIONS: GraphOptionItem[] = [
{
label: "Voice Agent Gemini 2.0 Realtime",
value: "va_gemini_v2v",
},
{
label: "Voice Agent with Dify",
value: "va_dify_azure",
},
{
label: "Voice Agent / STT + LLM + TTS",
value: "va_openai_azure",
Expand All @@ -76,10 +90,6 @@ export const GRAPH_OPTIONS: GraphOptionItem[] = [
label: "Voice Agent Coze Bot + Azure TTS",
value: "va_coze_azure",
},
{
label: "Voice Agent Gemini 2.0 Realtime",
value: "va_gemini_v2v",
},
]

export const isRagGraph = (graphName: string) => {
Expand Down
3 changes: 3 additions & 0 deletions demo/src/common/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface StartRequestConfig {
coze_token?: string
coze_bot_id?: string
coze_base_url?: string
dify_api_key?: string
}

interface GenAgoraDataConfig {
Expand Down Expand Up @@ -50,6 +51,7 @@ export const apiStartService = async (
coze_token,
coze_bot_id,
coze_base_url,
dify_api_key,
} = config
const data = {
request_id: genUUID(),
Expand All @@ -63,6 +65,7 @@ export const apiStartService = async (
coze_token: coze_token ?? undefined,
coze_bot_id: coze_bot_id ?? undefined,
coze_base_url: coze_base_url ?? undefined,
dify_api_key: dify_api_key ?? undefined
}
let resp: any = await axios.post(url, data)
resp = resp.data || {}
Expand Down
20 changes: 19 additions & 1 deletion demo/src/common/storage.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import { IAgentSettings, IOptions, ICozeSettings } from "@/types"
import { IAgentSettings, IOptions, ICozeSettings, IDifySettings } from "@/types"
import {
OPTIONS_KEY,
DEFAULT_OPTIONS,
AGENT_SETTINGS_KEY,
DEFAULT_AGENT_SETTINGS,
COZE_SETTINGS_KEY,
DEFAULT_COZE_SETTINGS,
DIFY_SETTINGS_KEY,
DEFAULT_DIFY_SETTINGS,
} from "./constant"

export const getOptionsFromLocal = (): {
options: IOptions
settings: IAgentSettings
cozeSettings: ICozeSettings
difySettings: IDifySettings
} => {
let data = {
options: DEFAULT_OPTIONS,
settings: DEFAULT_AGENT_SETTINGS,
cozeSettings: DEFAULT_COZE_SETTINGS,
difySettings: DEFAULT_DIFY_SETTINGS,
}
if (typeof window !== "undefined") {
const options = localStorage.getItem(OPTIONS_KEY)
Expand All @@ -31,6 +35,10 @@ export const getOptionsFromLocal = (): {
if (cozeSettings) {
data.cozeSettings = JSON.parse(cozeSettings)
}
const difySettings = localStorage.getItem(DIFY_SETTINGS_KEY)
if (difySettings) {
data.difySettings = JSON.parse(difySettings)
}
}
return data
}
Expand All @@ -53,6 +61,12 @@ export const setCozeSettingsToLocal = (settings: ICozeSettings) => {
}
}

export const setDifySettingsToLocal = (settings: IDifySettings) => {
if (typeof window !== "undefined") {
localStorage.setItem(DIFY_SETTINGS_KEY, JSON.stringify(settings))
}
}

export const resetSettingsByKeys = (keys: string | string[]) => {
if (typeof window !== "undefined") {
if (Array.isArray(keys)) {
Expand All @@ -68,3 +82,7 @@ export const resetSettingsByKeys = (keys: string | string[]) => {
export const resetCozeSettings = () => {
resetSettingsByKeys(COZE_SETTINGS_KEY)
}

export const resetDifySettings = () => {
resetSettingsByKeys(DIFY_SETTINGS_KEY)
}
Loading

0 comments on commit d5dba93

Please sign in to comment.