-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dify.AI | Create Chat Message Full Implementation
- Loading branch information
1 parent
392ec68
commit 1c7b2d4
Showing
8 changed files
with
122 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { createAction, option } from '@typebot.io/forge' | ||
import { isDefined, isEmpty } from '@typebot.io/lib' | ||
import { got } from 'got' | ||
import { auth } from '../auth' | ||
import { DifyResponse } from '../types' | ||
|
||
export const createChatMessage = createAction({ | ||
auth, | ||
name: 'Create Chat Message', | ||
options: option.object({ | ||
inputs: option.string.layout({ | ||
label: 'Inputs', | ||
helperText: | ||
'Variable values to be replaced in the prompt. The code must be a JSON object containing the variable key/value pairs.', | ||
inputType: 'code', | ||
defaultValue: '{}', | ||
}), | ||
query: option.string.layout({ | ||
label: 'Query', | ||
placeholder: 'User input/question content', | ||
inputType: 'textarea', | ||
isRequired: true, | ||
}), | ||
conversation_id: option.string.layout({ | ||
label: 'Conversation ID', | ||
moreInfoTooltip: | ||
'Used to remember the conversation with the user. If empty, a new conversation id is created.', | ||
}), | ||
user: option.string.layout({ | ||
label: 'User', | ||
moreInfoTooltip: | ||
'The user identifier, defined by the developer, must ensure uniqueness within the app.', | ||
}), | ||
responseMapping: option | ||
.saveResponseArray(['Answer', 'Conversation ID', 'Total Tokens']) | ||
.layout({ | ||
accordion: 'Save response', | ||
}), | ||
}), | ||
getSetVariableIds: ({ responseMapping }) => | ||
responseMapping?.map((r) => r.variableId).filter(isDefined) ?? [], | ||
run: { | ||
server: async ({ | ||
credentials: { apiEndpoint, apiKey }, | ||
options: { conversation_id, query, user, inputs, responseMapping }, | ||
variables, | ||
}) => { | ||
if (isEmpty(inputs)) inputs = '{}' | ||
|
||
const res: DifyResponse = await got | ||
.post(apiEndpoint + '/chat-messages', { | ||
headers: { | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
json: { | ||
inputs: JSON.parse(inputs), | ||
query, | ||
response_mode: 'blocking', | ||
conversation_id, | ||
user, | ||
}, | ||
}) | ||
.json() | ||
|
||
responseMapping?.forEach((mapping) => { | ||
if (!mapping.variableId) return | ||
|
||
const item = mapping.item ?? 'Answer' | ||
if (item === 'Answer') variables.set(mapping.variableId, res.answer) | ||
|
||
if (item === 'Conversation ID') | ||
variables.set(mapping.variableId, res.conversation_id) | ||
|
||
if (item === 'Total Tokens') | ||
variables.set(mapping.variableId, res.metadata.usage.total_tokens) | ||
}) | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { createBlock } from '@typebot.io/forge' | ||
import { DifyAiLogo } from './logo' | ||
import { auth } from './auth' | ||
import { createChatMessage } from './actions/createChatMessage' | ||
|
||
export const difyAi = createBlock({ | ||
id: 'dify-ai', | ||
name: 'Dify.AI', | ||
tags: ['dify', 'ai', 'documents', 'files', 'knowledge base'], | ||
LightLogo: DifyAiLogo, | ||
auth, | ||
actions: [], | ||
actions: [createChatMessage], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
import React from 'react' | ||
|
||
export const DifyAiLogo = (props: React.SVGProps<SVGSVGElement>) => <svg></svg> | ||
export const DifyAiLogo = (props: React.SVGProps<SVGSVGElement>) => ( | ||
<svg | ||
viewBox="0 0 25 25" | ||
width="25" | ||
height="25" | ||
xmlns="http://www.w3.org/2000/svg" | ||
{...props} | ||
> | ||
<image | ||
href="https://framerusercontent.com/images/xRJ6vNo9mUYeVNxt0KITXCXEuSk.png" | ||
height="25" | ||
width="25" | ||
/> | ||
</svg> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export type DifyResponse = { | ||
answer: string | ||
metadata: { | ||
usage: { | ||
total_tokens: number | ||
} | ||
} | ||
conversation_id: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.