Skip to content

Commit

Permalink
✨ Add OpenRouter block
Browse files Browse the repository at this point in the history
Closes #1254
  • Loading branch information
baptisteArno committed Mar 5, 2024
1 parent 77bc138 commit 84d6c59
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 3 deletions.
3 changes: 2 additions & 1 deletion apps/docs/openapi/builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -18793,7 +18793,8 @@
"dify-ai",
"mistral",
"elevenlabs",
"together-ai"
"together-ai",
"open-router"
]
},
"options": {}
Expand Down
3 changes: 2 additions & 1 deletion apps/docs/openapi/viewer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9414,7 +9414,8 @@
"dify-ai",
"mistral",
"elevenlabs",
"together-ai"
"together-ai",
"open-router"
]
},
"options": {}
Expand Down
50 changes: 50 additions & 0 deletions packages/forge/blocks/openRouter/actions/createChatCompletion.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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 { runChatCompletion } from '@typebot.io/openai-block/shared/runChatCompletion'
import { runChatCompletionStream } from '@typebot.io/openai-block/shared/runChatCompletionStream'
import { defaultOpenRouterOptions } from '../constants'
import { got } from 'got'
import { ModelsResponse } from '../types'

export const createChatCompletion = createAction({
name: 'Create chat completion',
auth,
options: parseChatCompletionOptions({
modelFetchId: 'fetchModels',
}),
getSetVariableIds: getChatCompletionSetVarIds,
fetchers: [
{
id: 'fetchModels',
dependencies: [],
fetch: async () => {
const response = await got
.get(defaultOpenRouterOptions.baseUrl + '/models')
.json<ModelsResponse>()

return response.data.map((model) => ({
value: model.id,
label: model.name,
}))
},
},
],
run: {
server: (params) =>
runChatCompletion({
...params,
config: { baseUrl: defaultOpenRouterOptions.baseUrl },
}),
stream: {
getStreamVariableId: getChatCompletionStreamVarId,
run: (params) =>
runChatCompletionStream({
...params,
config: { baseUrl: defaultOpenRouterOptions.baseUrl },
}),
},
},
})
15 changes: 15 additions & 0 deletions packages/forge/blocks/openRouter/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { option, AuthDefinition } from '@typebot.io/forge'

export const auth = {
type: 'encryptedCredentials',
name: 'OpenRouter account',
schema: option.object({
apiKey: option.string.layout({
label: 'API key',
isRequired: true,
inputType: 'password',
helperText:
'You can generate an API key [here](https://openrouter.ai/keys).',
}),
}),
} satisfies AuthDefinition
3 changes: 3 additions & 0 deletions packages/forge/blocks/openRouter/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const defaultOpenRouterOptions = {
baseUrl: 'https://openrouter.ai/api/v1',
} as const
13 changes: 13 additions & 0 deletions packages/forge/blocks/openRouter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createBlock } from '@typebot.io/forge'
import { OpenRouterLogo } from './logo'
import { auth } from './auth'
import { createChatCompletion } from './actions/createChatCompletion'

export const openRouter = createBlock({
id: 'open-router',
name: 'OpenRouter',
tags: ['ai', 'openai', 'chat', 'completion'],
LightLogo: OpenRouterLogo,
auth,
actions: [createChatCompletion],
})
23 changes: 23 additions & 0 deletions packages/forge/blocks/openRouter/logo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react'

export const OpenRouterLogo = (props: React.SVGProps<SVGSVGElement>) => (
<svg viewBox="0 0 512 512" fill="#71717A" stroke="#71717A" {...props}>
<g clip-path="url(#clip0_205_3)">
<path
d="M3 248.945C18 248.945 76 236 106 219C136 202 136 202 198 158C276.497 102.293 332 120.945 423 120.945"
strokeWidth="90"
></path>
<path d="M511 121.5L357.25 210.268L357.25 32.7324L511 121.5Z"></path>
<path
d="M0 249C15 249 73 261.945 103 278.945C133 295.945 133 295.945 195 339.945C273.497 395.652 329 377 420 377"
strokeWidth="90"
></path>
<path d="M508 376.445L354.25 287.678L354.25 465.213L508 376.445Z"></path>
</g>
<defs>
<clipPath id="clip0_205_3">
<rect width="512" height="512"></rect>
</clipPath>
</defs>
</svg>
)
17 changes: 17 additions & 0 deletions packages/forge/blocks/openRouter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@typebot.io/open-router-block",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"keywords": [],
"license": "ISC",
"devDependencies": {
"@typebot.io/forge": "workspace:*",
"@typebot.io/tsconfig": "workspace:*",
"@types/react": "18.2.15",
"typescript": "5.3.2",
"@typebot.io/lib": "workspace:*",
"@typebot.io/openai-block": "workspace:*",
"got": "12.6.0"
}
}
10 changes: 10 additions & 0 deletions packages/forge/blocks/openRouter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@typebot.io/tsconfig/base.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"lib": ["ESNext", "DOM"],
"noEmit": true,
"jsx": "react"
}
}
6 changes: 6 additions & 0 deletions packages/forge/blocks/openRouter/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type ModelsResponse = {
data: {
id: string
name: string
}[]
}
1 change: 1 addition & 0 deletions packages/forge/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export const enabledBlocks = [
'mistral',
'elevenlabs',
'together-ai',
'open-router',
] as const
2 changes: 2 additions & 0 deletions packages/forge/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Do not edit this file manually
import { openRouter } from '@typebot.io/open-router-block'
import { togetherAi } from '@typebot.io/together-ai-block'
import { elevenlabs } from '@typebot.io/elevenlabs-block'
import { difyAi } from '@typebot.io/dify-ai-block'
Expand Down Expand Up @@ -26,6 +27,7 @@ export const forgedBlocks = [
mistral,
elevenlabs,
togetherAi,
openRouter,
] as BlockDefinition<(typeof enabledBlocks)[number], any, any>[]

export type ForgedBlockDefinition = (typeof forgedBlocks)[number]
Expand Down
3 changes: 2 additions & 1 deletion packages/forge/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@typebot.io/dify-ai-block": "workspace:*",
"@typebot.io/mistral-block": "workspace:*",
"@typebot.io/elevenlabs-block": "workspace:*",
"@typebot.io/together-ai-block": "workspace:*"
"@typebot.io/together-ai-block": "workspace:*",
"@typebot.io/open-router-block": "workspace:*"
}
}
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 84d6c59

Please sign in to comment.