tegnike / aituber-kit

AITuber Kit
MIT License
143 stars 24 forks source link

Difyでテキストを直接送信すると1文目しか読まれない #80

Open tegnike opened 1 week ago

tegnike commented 6 days ago

@coderabbit Difyでテキストを直接送信すると1文目しか読まれないので、解決策を提示してください

tegnike commented 6 days ago

@coderabbitai Difyでテキストを直接送信すると1文目しか読まれないので、解決策を提示してください

coderabbitai[bot] commented 6 days ago

[!TIP] For best results, initiate chat on the files or code changes.

@tegnike, 現在のリポジトリ内で「Dify」に関連するテキスト処理を行っているコードを特定することができませんでした。問題の解決には、具体的にどのファイルやコードセグメントが関与しているかを特定する必要があります。もし可能であれば、Difyのテキストを送信している部分のコードを提供していただけますか?それにより、より具体的な解決策を提示することが可能になります。

Analysis chain ---
Scripts executed The following scripts were executed for the analysis: --- Script: ```shell #!/bin/bash # Find files related to 'Dify' text processing fd --type f --extension js --extension py "Dify" | xargs grep -n "sendText" --color=always ``` Length of output: 88 --- --- Script: ```shell #!/bin/bash # Broaden the search for any files related to text processing for Dify, case-insensitive fd --type f | xargs grep -i "Dify" | grep -i "send" ``` Length of output: 49
tegnike commented 6 days ago

@coderabbitai dify でもいいですよ

coderabbitai[bot] commented 6 days ago

[!TIP] For best results, initiate chat on the files or code changes.

@tegnike, 残念ながら、JavaScriptやPythonファイル内でテキスト送信に関連するコードを見つけることができませんでした。この機能が異なるプログラミング言語で実装されている可能性があります。または、異なるキーワードが使用されている可能性があります。

リポジトリ内の他のファイルタイプを調査するか、使用している具体的な関数名やプロセスに関する詳細を教えていただけますか?それにより、問題の原因を特定しやすくなります。

Analysis chain
Scripts executed The following scripts were executed for the analysis: --- Script: ```shell #!/bin/bash # Broaden the search to find any code related to sending or processing text, regardless of case fd --type f --extension js --extension py | xargs grep -i "send.*text" ``` Length of output: 72
github-actions[bot] commented 4 days ago

Issue分析結果:

以下に、このIssueに関連すると考えられるファイルをJSON形式で出力します:

[ { "file_path": "src/features/chat/difyChat.ts", "reason": "DifyのChat機能を実装しているファイルであり、テキスト送信の処理が含まれている可能性が高いため。" }, { "file_path": "src/features/chat/aiChatFactory.ts", "reason": "様々なAIサービスとのやり取りを管理しており、Difyの処理も含まれている可能性があるため。" }, { "file_path": "src/pages/index.tsx", "reason": "アプリケーションのメインコンポーネントであり、チャット機能の統合部分が含まれているため。" }, { "file_path": "src/components/messageInput.tsx", "reason": "メッセージ入力コンポーネントであり、テキスト送信の処理が含まれている可能性があるため。" }, { "file_path": "src/components/messageInputContainer.tsx", "reason": "メッセージ入力を管理するコンテナコンポーネントであり、テキスト送信の処理が含まれている可能性があるため。" }, { "file_path": "src/features/messages/messages.ts", "reason": "メッセージ関連の型定義や処理が含まれており、テキスト送信に関連している可能性があるため。" }, { "file_path": "src/components/menu.tsx", "reason": "アプリケーションの設定や機能を管理するコンポーネントであり、Difyの設定が含まれている可能性があるため。" } ]

これらのファイルは、Difyチャット機能の実装や、テキスト送信の処理に関連している可能性が高いため、問題の原因を特定するのに役立つかもしれません。

github-actions[bot] commented 4 days ago

Issue分析結果:

[ { "file_path": "src/features/chat/difyChat.ts", "reason": "\u3053\u306e\u30d5\u30a1\u30a4\u30eb\u306fDify API\u3092\u4f7f\u7528\u3057\u3066\u30c1\u30e3\u30c3\u30c8\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002Issue\u3067\u5831\u544a\u3055\u308c\u3066\u3044\u308b\u554f\u984c\u304c\u3001\u3053\u306eAPI\u3068\u306e\u901a\u4fe1\u3084\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u51e6\u7406\u306b\u95a2\u9023\u3057\u3066\u3044\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002" }, { "file_path": "src/features/chat/aiChatFactory.ts", "reason": "\u3053\u306e\u30d5\u30a1\u30a4\u30eb\u306f\u69d8\u3005\u306aAI\u30b5\u30fc\u30d3\u30b9\uff08Dify\u3092\u542b\u3080\uff09\u3068\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u3059\u3002Dify\u304b\u3089\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u51e6\u7406\u306b\u554f\u984c\u304c\u3042\u308b\u5834\u5408\u3001\u3053\u306e\u90e8\u5206\u3092\u78ba\u8a8d\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002" }, { "file_path": "src/pages/index.tsx", "reason": "\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30e1\u30a4\u30f3\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u3042\u308a\u3001\u30c1\u30e3\u30c3\u30c8\u6a5f\u80fd\u306e\u7d71\u5408\u304c\u884c\u308f\u308c\u3066\u3044\u307e\u3059\u3002\u30c6\u30ad\u30b9\u30c8\u5165\u529b\u3068AI\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u51e6\u7406\u30ed\u30b8\u30c3\u30af\u304c\u3053\u3053\u306b\u542b\u307e\u308c\u3066\u3044\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u3067\u3059\u3002" }, { "file_path": "src/components/messageInput.tsx", "reason": "\u30e6\u30fc\u30b6\u30fc\u306e\u30c6\u30ad\u30b9\u30c8\u5165\u529b\u3092\u51e6\u7406\u3059\u308b\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u3059\u3002\u5165\u529b\u30c6\u30ad\u30b9\u30c8\u306e\u51e6\u7406\u65b9\u6cd5\u306b\u554f\u984c\u304c\u3042\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002" }, { "file_path": "src/components/chatLog.tsx", "reason": "\u30c1\u30e3\u30c3\u30c8\u30ed\u30b0\u3092\u8868\u793a\u3059\u308b\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u3059\u3002AI\u304b\u3089\u306e\u5fdc\u7b54\u304c\u6b63\u3057\u304f\u8868\u793a\u3055\u308c\u3066\u3044\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002" } ]

コード改善案:

Based on the provided code and the issue description, the problem seems to be in the getDifyChatResponseStream function in src/features/chat/difyChat.ts. The function is only sending the last message's content instead of the full conversation history. Let's modify this function to include the entire conversation history:

// src/features/chat/difyChat.ts

export async function getDifyChatResponseStream(
  messages: Message[],
  apiKey: string,
  url: string,
  conversationId: string,
  setDifyConversationId: (id: string) => void
) {
  if (!apiKey) {
    throw new Error("Invalid API Key");
  }

  const headers = {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json'
  };

  // Create an array of message objects for the entire conversation history
  const conversationHistory = messages.map(message => ({
    role: message.role,
    content: message.content
  }));

  const body = JSON.stringify({
    inputs: {},
    query: messages[messages.length - 1].content, // Keep the latest message as the query
    response_mode: "streaming",
    conversation_id: conversationId,
    user: "aituber-kit",
    files: [],
    conversation_history: conversationHistory // Add the entire conversation history
  });

  // ... (rest of the function remains the same)
}

Explanation of the changes:

  1. We create a conversationHistory array that includes all messages from the conversation, mapping them to the format expected by the Dify API (with role and content properties).

  2. We add this conversationHistory to the request body under the conversation_history key.

  3. We keep the query as the content of the last message, which represents the user's most recent input.

These changes should allow the Dify API to consider the entire conversation context when generating responses, rather than just the last message. This should resolve the issue of only the first sentence being read.

Additionally, you may want to update the aiChatFactory.ts file to ensure it's passing the full message history to the getDifyChatResponseStream function:

// src/features/chat/aiChatFactory.ts

case "dify":
  return getDifyChatResponseStream(
    messages, // Pass the entire messages array
    config.dify.key,
    config.dify.url,
    config.dify.conversationId,
    config.dify.setConversationId
  );

These modifications should address the issue of Dify only reading the first sentence by providing the full conversation context to the API.

github-actions[bot] commented 4 days ago

Issue分析結果:

[
  {
    "file_path": "src/features/chat/difyChat.ts",
    "reason": "Difyチャットの応答を処理する機能が実装されているため、1文目しか読まれない問題が発生している可能性があります。"
  },
  {
    "file_path": "src/features/chat/aiChatFactory.ts",
    "reason": "各種AIサービスとのやり取りを管理しており、Difyの応答処理にも関わっている可能性があります。"
  },
  {
    "file_path": "src/pages/index.tsx",
    "reason": "アプリケーションのメインコンポーネントであり、チャット処理のロジックが含まれているため、問題の原因がここにある可能性があります。"
  },
  {
    "file_path": "src/components/chatLog.tsx",
    "reason": "チャットログの表示を担当しているコンポーネントであり、メッセージの表示方法に問題がある可能性があります。"
  },
  {
    "file_path": "src/features/messages/messages.ts",
    "reason": "メッセージの処理や変換を行っている可能性があり、Difyからの応答の解析に問題がある可能性があります。"
  },
  {
    "file_path": "src/components/menu.tsx",
    "reason": "チャットUIのコンポーネントを提供しており、Difyの設定や応答の表示に関連している可能性があります。"
  },
  {
    "file_path": "src/components/messageInputContainer.tsx",
    "reason": "メッセージ入力と送信を管理しているコンポーネントであり、Difyへの入力処理に問題がある可能性があります。"
  }
]

コード改善案:

提供されたコードを分析した結果、Difyでテキストを直接送信すると1文目しか読まれない問題の原因と改善案について説明します。

問題の原因: src/features/chat/difyChat.ts ファイルの getDifyChatResponseStream 関数内で、Dify APIにリクエストを送信する際に、query パラメータに最後のメッセージの内容のみを設定しています。これにより、複数の文を含むメッセージの場合、最初の文しか処理されない可能性があります。

const body = JSON.stringify({
  inputs: {},
  query: messages[messages.length - 1].content,
  // ...
});

改善案:

  1. Dify APIに送信するクエリに、すべての関連するメッセージを含めるようにします。

  2. getDifyChatResponseStream 関数を以下のように修正します:

export async function getDifyChatResponseStream(
  messages: Message[],
  apiKey: string,
  url: string,
  conversationId: string,
  setDifyConversationId: (id: string) => void
) {
  // ... (既存のコード)

  const body = JSON.stringify({
    inputs: {},
    query: messages.map(msg => msg.content).join('\n'), // すべてのメッセージを結合
    response_mode: "streaming",
    conversation_id: conversationId,
    user: "aituber-kit",
    files: []
  });

  // ... (残りのコード)
}

この修正により、Dify APIに送信されるクエリには、すべてのメッセージの内容が含まれるようになります。これにより、複数の文を含むメッセージや、会話の文脈全体が適切に処理されるようになります。

また、必要に応じて、Dify APIの仕様に合わせて、メッセージの形式やパラメータを調整することも検討してください。例えば、各メッセージのロール(user, assistant, system)を明示的に指定する必要がある場合は、それに応じてリクエストボディを調整してください。

これらの変更を適用することで、Difyでテキストを直接送信した際に、すべての文が適切に処理されるようになると期待できます。

github-actions[bot] commented 4 days ago

Issue分析結果:

[
  {
    "file_path": "src/features/chat/difyChat.ts",
    "reason": "DifyチャットのレスポンスストリームをSNSするための関数が定義されているファイルです。Difyの応答処理に関連している可能性があります。"
  },
  {
    "file_path": "src/features/chat/aiChatFactory.ts",
    "reason": "様々なAIサービス(Difyを含む)とのやり取りを行うファクトリクラスが定義されています。Difyの応答処理に関連している可能性があります。"
  },
  {
    "file_path": "src/components/menu.tsx",
    "reason": "Difyの設定(APIキー、URL、conversationID)を変更するハンドラーが定義されています。Difyの設定に関連している可能性があります。"
  },
  {
    "file_path": "src/pages/index.tsx",
    "reason": "アプリケーションのメインコンポーネントが定義されており、AIレスポンスの処理や送信などの機能が実装されています。Difyの応答処理に関連している可能性があります。"
  },
  {
    "file_path": "src/features/messages/messages.ts",
    "reason": "メッセージの処理や感情表現の生成に関する機能が定義されています。Difyからの応答の処理に関連している可能性があります。"
  },
  {
    "file_path": "src/components/chatLog.tsx",
    "reason": "チャットログの表示を行うコンポーネントが定義されています。Difyからの応答の表示に関連している可能性があります。"
  },
  {
    "file_path": "src/components/settings.tsx",
    "reason": "アプリケーションの設定画面を定義しているコンポーネントです。Difyの設定に関連している可能性があります。"
  },
  {
    "file_path": "src/features/messages/speakCharacter.ts",
    "reason": "音声合成APIを使用して音声データを取得し、VRMモデルに話させる機能を提供しています。Difyからの応答を音声に変換する処理に関連している可能性があります。"
  },
  {
    "file_path": "src/components/messageInput.tsx",
    "reason": "ユーザーの入力フィールドとボタンのコンポーネントを定義しています。Difyへのメッセージ送信に関連している可能性があります。"
  },
  {
    "file_path": "src/components/messageInputContainer.tsx",
    "reason": "テキスト入力と音声入力を提供するコンポーネントです。Difyへのメッセージ送信に関連している可能性があります。"
  }
]

コード改善案:

DifyでテキストをAPIへ送信する際に、1文目しか読まれないという問題を解決するためには、src/features/chat/difyChat.tsファイルのgetDifyChatResponseStream関数を修正する必要があります。以下に具体的な改善案を示します。

  1. メッセージの全文を送信する: 現在のコードでは、最後のメッセージの内容のみを送信しています。これを全てのメッセージを含むように変更します。

  2. 会話の履歴を保持する: Difyは会話の文脈を理解するために、過去のメッセージも必要とします。そのため、会話の履歴を送信するようにします。

以下が改善されたコードです:

export async function getDifyChatResponseStream(
  messages: Message[],
  apiKey: string,
  url: string,
  conversationId: string,
  setDifyConversationId: (id: string) => void
) {
  if (!apiKey) {
    throw new Error("Invalid API Key");
  }

  const headers = {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json'
  };

  // 会話の履歴を作成
  const conversation = messages.map(msg => ({
    role: msg.role,
    content: msg.content
  }));

  const body = JSON.stringify({
    inputs: {},
    query: messages[messages.length - 1].content,
    response_mode: "streaming",
    conversation_id: conversationId,
    user: "aituber-kit",
    conversation: conversation, // 会話の履歴を追加
    files: []
  });

  const response = await fetch(url, {
    method: 'POST',
    headers: headers,
    body: body
  });

  // 以下、既存のコードは変更なし
  // ...
}

この改善により、以下の効果が期待できます:

  1. Difyに送信される情報に会話の全履歴が含まれるため、文脈を考慮した応答が可能になります。

  2. 1文目だけでなく、会話全体が考慮されるため、より適切な応答が得られるようになります。

  3. conversation_idを利用することで、連続した会話のセッションを維持できます。

また、この変更に合わせて、src/pages/index.tsxhandleSendChat関数内でDifyを使用する部分も、会話の履歴を正しく渡すように修正する必要があります。

// src/pages/index.tsx 内の handleSendChat 関数の一部

if (selectAIService === "dify") {
  const messages = [
    { role: "system", content: systemPrompt },
    ...chatLog,
    { role: "user", content: newMessage }
  ];

  await getDifyChatResponseStream(
    messages,
    difyKey,
    difyUrl,
    difyConversationId,
    setDifyConversationId
  );
}

これらの変更により、Difyを使用する際に全ての会話履歴が考慮され、1文目だけでなく文脈を理解した応答が得られるようになります。