mirror of
https://github.com/netbirdio/explain.git
synced 2026-05-22 18:44:28 -07:00
Add dify as a provider
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import type { Message } from "../types";
|
||||
import { AnthropicProvider } from "./providers/anthropic";
|
||||
import { DifyProvider } from "./providers/dify";
|
||||
import { OpenAIProvider } from "./providers/openai";
|
||||
import type { LLMProvider, Middleware } from "./providers/types";
|
||||
|
||||
export type AssistantConfig = {
|
||||
provider: "anthropic" | "openai" | LLMProvider;
|
||||
provider: "anthropic" | "openai" | "dify" | LLMProvider;
|
||||
apiKey?: string;
|
||||
model?: string;
|
||||
baseUrl?: string;
|
||||
systemPrompt?: string;
|
||||
middleware?: Middleware[];
|
||||
};
|
||||
@@ -105,6 +107,8 @@ function createProvider(config: AssistantConfig): LLMProvider {
|
||||
return new AnthropicProvider({ apiKey: config.apiKey, model: config.model });
|
||||
case "openai":
|
||||
return new OpenAIProvider({ apiKey: config.apiKey, model: config.model });
|
||||
case "dify":
|
||||
return new DifyProvider({ apiKey: config.apiKey, baseUrl: config.baseUrl });
|
||||
default:
|
||||
throw new Error(`Unknown provider: ${config.provider}`);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export { createAssistant } from "./handler";
|
||||
export type { AssistantConfig } from "./handler";
|
||||
export { AnthropicProvider } from "./providers/anthropic";
|
||||
export { DifyProvider } from "./providers/dify";
|
||||
export { OpenAIProvider } from "./providers/openai";
|
||||
export type { LLMProvider, ProviderConfig, Middleware } from "./providers/types";
|
||||
@@ -0,0 +1,66 @@
|
||||
import type { Message } from "../../types";
|
||||
import type { LLMProvider, ProviderConfig } from "./types";
|
||||
|
||||
export class DifyProvider implements LLMProvider {
|
||||
private apiKey: string;
|
||||
private baseUrl: string;
|
||||
|
||||
constructor(config: ProviderConfig) {
|
||||
this.apiKey = config.apiKey;
|
||||
if (!config.baseUrl) {
|
||||
throw new Error("baseUrl is required for the Dify provider");
|
||||
}
|
||||
this.baseUrl = config.baseUrl.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
async chat(messages: Message[], systemPrompt?: string): Promise<string> {
|
||||
const query = this.buildQuery(messages, systemPrompt);
|
||||
|
||||
const response = await fetch(`${this.baseUrl}/v1/chat-messages`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${this.apiKey}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
inputs: {},
|
||||
query,
|
||||
response_mode: "blocking",
|
||||
user: "explain-user",
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(`Dify API error ${response.status}: ${text}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
if (!data.answer) {
|
||||
throw new Error("No answer in Dify response");
|
||||
}
|
||||
return data.answer;
|
||||
}
|
||||
|
||||
private buildQuery(messages: Message[], systemPrompt?: string): string {
|
||||
const parts: string[] = [];
|
||||
|
||||
if (systemPrompt) {
|
||||
parts.push(`[System]: ${systemPrompt}`);
|
||||
}
|
||||
|
||||
for (const m of messages) {
|
||||
if (m.role === "context") {
|
||||
parts.push(`[Context]: ${m.content}`);
|
||||
} else if (m.role === "system") {
|
||||
parts.push(`[System]: ${m.content}`);
|
||||
} else if (m.role === "assistant") {
|
||||
parts.push(`[Assistant]: ${m.content}`);
|
||||
} else {
|
||||
parts.push(m.content);
|
||||
}
|
||||
}
|
||||
|
||||
return parts.join("\n\n");
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ export interface LLMProvider {
|
||||
export type ProviderConfig = {
|
||||
apiKey: string;
|
||||
model?: string;
|
||||
baseUrl?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user