2
0

Add Groq block

Closes #1721
This commit is contained in:
Baptiste Arnaud
2024-08-22 15:31:05 +02:00
parent 77614f671f
commit 6a7877dc9b
22 changed files with 959 additions and 45 deletions

View File

@ -68,10 +68,7 @@ const configs = [
]
function onwarn(warning, warn) {
if (
warning.code === 'CIRCULAR_DEPENDENCY' &&
warning.ids.some((id) => id.includes('@internationalized+date'))
) {
if (warning.code === 'CIRCULAR_DEPENDENCY') {
return
}

View File

@ -0,0 +1,81 @@
import { createAction } from '@typebot.io/forge'
import { auth } from '../auth'
import { parseChatCompletionOptions } from '@typebot.io/openai-block/shared/parseChatCompletionOptions'
import { getChatCompletionSetVarIds } from '@typebot.io/openai-block/shared/getChatCompletionSetVarIds'
import { getChatCompletionStreamVarId } from '@typebot.io/openai-block/shared/getChatCompletionStreamVarId'
import { runOpenAIChatCompletion } from '@typebot.io/openai-block/shared/runOpenAIChatCompletion'
import { runOpenAIChatCompletionStream } from '@typebot.io/openai-block/shared/runOpenAIChatCompletionStream'
import { defaultBaseUrl, defaultTemperature } from '../constants'
import ky from 'ky'
export const createChatCompletion = createAction({
name: 'Create chat completion',
auth,
options: parseChatCompletionOptions({
modelFetchId: 'fetchModels',
defaultTemperature,
}),
fetchers: [
{
id: 'fetchModels',
fetch: async ({ credentials }) => {
if (!credentials?.apiKey) return []
const response = await ky
.get(`${defaultBaseUrl}/models`, {
headers: {
authorization: `Bearer ${credentials.apiKey}`,
},
})
.json<{ data: { id: string; created: number }[] }>()
return response.data
.sort((a, b) => b.created - a.created)
.map((model) => model.id)
},
dependencies: [],
},
],
turnableInto: [
{
blockId: 'openai',
},
{
blockId: 'open-router',
},
{ blockId: 'mistral' },
{
blockId: 'anthropic',
transform: (options) => ({
...options,
action: 'Create Chat Message',
responseMapping: options.responseMapping?.map((res: any) =>
res.item === 'Message content'
? { ...res, item: 'Message Content' }
: res
),
}),
},
{
blockId: 'together-ai',
},
],
getSetVariableIds: getChatCompletionSetVarIds,
run: {
server: (params) =>
runOpenAIChatCompletion({
...params,
config: { baseUrl: defaultBaseUrl },
}),
stream: {
getStreamVariableId: getChatCompletionStreamVarId,
run: async (params) =>
runOpenAIChatCompletionStream({
...params,
config: {
baseUrl: defaultBaseUrl,
},
}),
},
},
})

View File

@ -0,0 +1,24 @@
import { option, AuthDefinition } from '@typebot.io/forge'
import { defaultBaseUrl } from './constants'
export const auth = {
type: 'encryptedCredentials',
name: 'Groq account',
schema: option.object({
apiKey: option.string.layout({
label: 'API key',
isRequired: true,
inputType: 'password',
helperText:
'You can generate an API key [here](https://console.groq.com/keys).',
withVariableButton: false,
isDebounceDisabled: true,
}),
baseUrl: option.string.layout({
label: 'Base URL',
defaultValue: defaultBaseUrl,
withVariableButton: false,
isDebounceDisabled: true,
}),
}),
} satisfies AuthDefinition

View File

@ -0,0 +1,3 @@
export const defaultBaseUrl = 'https://api.groq.com/openai/v1'
export const defaultTemperature = 1

View File

@ -0,0 +1,13 @@
import { createBlock } from '@typebot.io/forge'
import { GroqLogo } from './logo'
import { auth } from './auth'
import { createChatCompletion } from './actions/createChatCompletion'
export const groqBlock = createBlock({
id: 'groq',
name: 'Groq',
tags: ['ai', 'chat completion', 'bot'],
LightLogo: GroqLogo,
auth,
actions: [createChatCompletion],
})

View File

@ -0,0 +1,14 @@
/** @jsxImportSource react */
export const GroqLogo = (props: React.SVGProps<SVGSVGElement>) => (
<svg viewBox="0 0 24 24" fill="none" {...props}>
<path
fill="#F55036"
d="M24 12c0 6.627-5.373 12-12 12S0 18.627 0 12 5.373 0 12 0s12 5.373 12 12Z"
></path>
<path
fill="#fff"
d="M12.002 6a4.118 4.118 0 0 0-4.118 4.108 4.118 4.118 0 0 0 4.118 4.109h1.354v-1.541h-1.354a2.574 2.574 0 0 1-2.574-2.568 2.574 2.574 0 0 1 2.574-2.568c1.42 0 2.58 1.152 2.58 2.568v3.784a2.58 2.58 0 0 1-2.555 2.567 2.558 2.558 0 0 1-1.791-.752l-1.092 1.09a4.095 4.095 0 0 0 2.855 1.202l.028.001h.029a4.118 4.118 0 0 0 4.061-4.09l.002-3.903A4.119 4.119 0 0 0 12.002 6Z"
></path>
</svg>
)

View File

@ -0,0 +1,19 @@
{
"name": "@typebot.io/groq-block",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"keywords": [],
"license": "AGPL-3.0-or-later",
"devDependencies": {
"@typebot.io/forge": "workspace:*",
"@typebot.io/lib": "workspace:*",
"@typebot.io/openai-block": "workspace:*",
"@typebot.io/tsconfig": "workspace:*",
"@types/react": "18.2.15",
"typescript": "5.4.5"
},
"dependencies": {
"ky": "1.2.4"
}
}

View File

@ -0,0 +1,10 @@
// Do not edit this file manually
import { parseBlockCredentials, parseBlockSchema } from '@typebot.io/forge'
import { groqBlock } from '.'
import { auth } from './auth'
export const groqBlockSchema = parseBlockSchema(groqBlock)
export const groqCredentialsSchema = parseBlockCredentials(
groqBlock.id,
auth.schema
)

View File

@ -0,0 +1,11 @@
{
"extends": "@typebot.io/tsconfig/base.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"lib": ["ESNext", "DOM"],
"noEmit": true,
"jsx": "preserve",
"jsxImportSource": "react"
}
}

View File

@ -1,9 +1,8 @@
import { option, createAction } from '@typebot.io/forge'
import { isDefined } from '@typebot.io/lib'
import { auth } from '../auth'
import { parseMessages } from '../helpers/parseMessages'
import { createMistral } from '@ai-sdk/mistral'
import { generateText, streamText } from 'ai'
import { generateText } from 'ai'
import { fetchModels } from '../helpers/fetchModels'
import { toolsSchema } from '@typebot.io/ai/schemas'
import { parseTools } from '@typebot.io/ai/parseTools'
@ -82,6 +81,13 @@ export const createChatCompletion = createAction({
model: undefined,
}),
},
{
blockId: 'groq',
transform: (opts) => ({
...opts,
model: undefined,
}),
},
{
blockId: 'together-ai',
},

View File

@ -16,6 +16,9 @@ export const createChatCompletion = createAction({
{
blockId: 'openai',
},
{
blockId: 'groq',
},
{
blockId: 'together-ai',
},

View File

@ -26,6 +26,7 @@ export const createChatCompletion = createAction({
blockId: 'together-ai',
},
{ blockId: 'mistral' },
{ blockId: 'groq' },
{
blockId: 'anthropic',
transform: (options) => ({

View File

@ -19,6 +19,8 @@ export const auth = createAuth({
defaultValue: 'https://api.openai.com/v1',
moreInfoTooltip:
'Use a different URL prefix for API calls, e.g. to use proxy servers.',
withVariableButton: false,
isDebounceDisabled: true,
}),
}),
})

View File

@ -35,6 +35,7 @@ export const createChatCompletion = createAction({
),
}),
},
{ blockId: 'groq' },
],
getSetVariableIds: getChatCompletionSetVarIds,
run: {

View File

@ -14,4 +14,5 @@ export const forgedBlockIds = [
'open-router',
'nocodb',
'segment',
'groq',
] as const satisfies ForgedBlock['type'][]

View File

@ -18,6 +18,8 @@ import { nocodbBlock } from '@typebot.io/nocodb-block'
import { nocodbCredentialsSchema } from '@typebot.io/nocodb-block/schemas'
import { segmentBlock } from '@typebot.io/segment-block'
import { segmentCredentialsSchema } from '@typebot.io/segment-block/schemas'
import { groqBlock } from '@typebot.io/groq-block'
import { groqCredentialsSchema } from '@typebot.io/groq-block/schemas'
export const forgedCredentialsSchemas = {
[openAIBlock.id]: openAICredentialsSchema,
@ -30,4 +32,5 @@ export const forgedCredentialsSchemas = {
[openRouterBlock.id]: openRouterCredentialsSchema,
[nocodbBlock.id]: nocodbCredentialsSchema,
[segmentBlock.id]: segmentCredentialsSchema,
[groqBlock.id]: groqCredentialsSchema,
}

View File

@ -11,6 +11,7 @@ import { calComBlock } from '@typebot.io/cal-com-block'
import { openAIBlock } from '@typebot.io/openai-block'
import { nocodbBlock } from '@typebot.io/nocodb-block'
import { segmentBlock } from '@typebot.io/segment-block'
import { groqBlock } from '@typebot.io/groq-block'
export const forgedBlocks = {
[openAIBlock.id]: openAIBlock,
@ -25,4 +26,5 @@ export const forgedBlocks = {
[openRouterBlock.id]: openRouterBlock,
[nocodbBlock.id]: nocodbBlock,
[segmentBlock.id]: segmentBlock,
[groqBlock.id]: groqBlock,
}

View File

@ -18,6 +18,7 @@
"@typebot.io/together-ai-block": "workspace:*",
"@typebot.io/open-router-block": "workspace:*",
"@typebot.io/nocodb-block": "workspace:*",
"@typebot.io/segment-block": "workspace:*"
"@typebot.io/segment-block": "workspace:*",
"@typebot.io/groq-block": "workspace:*"
}
}

View File

@ -23,6 +23,8 @@ import { nocodbBlock } from '@typebot.io/nocodb-block'
import { nocodbBlockSchema } from '@typebot.io/nocodb-block/schemas'
import { segmentBlock } from '@typebot.io/segment-block'
import { segmentBlockSchema } from '@typebot.io/segment-block/schemas'
import { groqBlock } from '@typebot.io/groq-block'
import { groqBlockSchema } from '@typebot.io/groq-block/schemas'
export const forgedBlockSchemas = {
[openAIBlock.id]: openAIBlockSchema,
@ -37,4 +39,5 @@ export const forgedBlockSchemas = {
[openRouterBlock.id]: openRouterBlockSchema,
[nocodbBlock.id]: nocodbBlockSchema,
[segmentBlock.id]: segmentBlockSchema,
[groqBlock.id]: groqBlockSchema,
}