This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
222 additions
and
81 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
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,4 +1,4 @@ | ||
{ | ||
// https://nuxt.com/docs/guide/concepts/typescript | ||
"extends": "./.nuxt/tsconfig.json" | ||
"extends": ["./.nuxt/tsconfig.json", "../tsconfig.json"] | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"packages/*", | ||
"skills/*", | ||
"docs", | ||
"oai" | ||
"server" | ||
], | ||
"resolutions": { | ||
"typescript": "^5.0.0", | ||
|
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 @@ | ||
generated/ |
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,3 @@ | ||
# @ava/oai-openapi | ||
|
||
[OAI OpenAPI](https://github.com/openai/openai-openapi) type definitions. |
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,4 @@ | ||
/** | ||
* Please run generate script to use this module, it should run on postinstall automatically | ||
*/ | ||
export * from './generated' |
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,16 @@ | ||
{ | ||
"name": "@ava/oai-openapi", | ||
"module": "index.ts", | ||
"type": "module", | ||
"scripts": { | ||
"generate": "bun run scripts/generate.ts", | ||
"postinstall": "bun --bun run generate" | ||
}, | ||
"devDependencies": { | ||
"@types/bun": "latest", | ||
"openapi-typescript-codegen": "^0.29.0" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^5.0.0" | ||
} | ||
} |
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,15 @@ | ||
import { $, write } from 'bun' | ||
import { mkdir } from 'fs/promises' | ||
import { join } from 'path' | ||
import { tmpdir } from 'os' | ||
|
||
const tmpDir = join(tmpdir(), '@ava/oai-openapi') | ||
await mkdir(tmpDir, { recursive: true }) | ||
const cwd = join(__dirname, '../generated') | ||
await mkdir(cwd, { recursive: true }) | ||
const res = await fetch('https://raw.githubusercontent.com/openai/openai-openapi/2.0.0/openapi.yaml') | ||
const def = await res.text() | ||
const dest = join(tmpDir, 'openapi.yaml') | ||
await write(dest, def) | ||
console.log(cwd, dest) | ||
await $`openapi --input ${dest} --output ${cwd}` |
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,3 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
} |
File renamed without changes.
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,15 @@ | ||
# Elysia with Bun runtime | ||
|
||
## Getting Started | ||
To get started with this template, simply paste this command into your terminal: | ||
```bash | ||
bun create elysia ./elysia-example | ||
``` | ||
|
||
## Development | ||
To start the development server run: | ||
```bash | ||
bun run dev | ||
``` | ||
|
||
Open http://localhost:3000/ with your browser to see the result. |
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,19 @@ | ||
import { Elysia } from 'elysia' | ||
import { swagger } from './services/swagger' | ||
import { autoload } from "elysia-autoload" | ||
import { PORT } from './services/config' | ||
import { elysiaLogger } from './services/logger' | ||
import { getAssistant } from './services/assistant' | ||
|
||
const assistant = await getAssistant() | ||
console.log('Assistant initialized', assistant.getName()) | ||
|
||
const app = new Elysia() | ||
.use(swagger) | ||
.use(elysiaLogger) | ||
.use(autoload()) | ||
.listen(PORT, (server) => { | ||
console.log(`listening on http://${server.hostname}:${server.port}`) | ||
}) | ||
|
||
export type ElysiaApp = typeof app |
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,3 @@ | ||
import type { ElysiaApp } from "~/index"; | ||
|
||
export default (app: ElysiaApp) => app.get('/', { ready: true }) |
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,12 @@ | ||
import type { CreateChatCompletionRequest, CreateChatCompletionResponse } from "@ava/oai-openapi"; | ||
import { t } from "elysia"; | ||
import type { ElysiaApp } from "~/index"; | ||
import { executeChatCompletion } from "~/services/oai/lang"; | ||
|
||
export default (app: ElysiaApp) => { | ||
return app.post('/', async ({ body }): Promise<CreateChatCompletionResponse> => { | ||
return executeChatCompletion(body as CreateChatCompletionRequest) | ||
}, { | ||
type: 'json' | ||
}) | ||
} |
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,19 @@ | ||
import { createAssistant, type ToolInterface } from "@ava/lang"; | ||
|
||
let assistant: Awaited<ReturnType<typeof createAssistant>> | null = null | ||
|
||
export const getAssistant = async () => { | ||
if (assistant) { | ||
return assistant | ||
} | ||
|
||
const tools: ToolInterface[] = [] | ||
|
||
if (process.env.HOMEASSISTANT) { | ||
const { createHomeAssistantToolkit } = await import('@ava/skills-homeassistant') | ||
tools.push(...await createHomeAssistantToolkit()) | ||
} | ||
|
||
assistant = await createAssistant(tools) | ||
return assistant | ||
} |
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,12 @@ | ||
import {parseEnv, port, z} from 'znv' | ||
|
||
export const { PORT, LOG_LEVEL } = parseEnv(process.env, { | ||
PORT: { | ||
schema: port().default(2881), | ||
description: 'Port for HTTP server to listen on' | ||
}, | ||
LOG_LEVEL: { | ||
schema: z.enum(["fatal", "error", "warn", "info", "debug", "trace"]).default('info'), | ||
description: 'Log level for the server' | ||
} | ||
}) |
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,7 @@ | ||
import { logger } from "@bogeychan/elysia-logger"; | ||
import { LOG_LEVEL } from "./config"; | ||
|
||
export const elysiaLogger = logger({ | ||
name: '@ava/server', | ||
level: LOG_LEVEL | ||
}) |
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,61 @@ | ||
import type { ChatCompletionRequestMessage, CreateChatCompletionRequest, CreateChatCompletionResponse } from "@ava/oai-openapi" | ||
import { v4 } from "uuid" | ||
import { getAssistant } from "../assistant" | ||
import { AIMessage, HumanMessage, SystemMessage } from "@ava/lang" | ||
|
||
const oaiToLangMessage = (messages: ChatCompletionRequestMessage[]) => { | ||
return messages.map(({role, content}) => { | ||
if(!content) return | ||
|
||
switch (role) { | ||
case 'user': | ||
return new HumanMessage(content) | ||
case 'assistant': | ||
return new AIMessage(content) | ||
case 'system': | ||
return new SystemMessage(content) | ||
default: | ||
return | ||
} | ||
}).filter(Boolean) | ||
} | ||
|
||
export const executeChatCompletion = async (req: CreateChatCompletionRequest): Promise<CreateChatCompletionResponse> => { | ||
const id = v4() | ||
const assistant = await getAssistant() | ||
|
||
if(req.stream) { | ||
throw new Error('streaming is not supported yet') | ||
} | ||
|
||
const lastMessage = req.messages[req.messages.length - 1] | ||
if (lastMessage.role !== 'user') { | ||
throw new Error('last message must be from user role') | ||
} | ||
const input = lastMessage.content | ||
const chat_history = oaiToLangMessage(req.messages.slice(0, -1)) | ||
|
||
|
||
const res = await assistant.invoke({ | ||
input, | ||
chat_history, | ||
}, { | ||
configurable: { | ||
sessionId: id | ||
} | ||
}) | ||
|
||
return { | ||
id, | ||
model: 'ava', | ||
choices: [ | ||
{ | ||
index: 0, | ||
finish_reason: 'stop', | ||
message: res.output, | ||
} | ||
], | ||
object: 'chat.completion', | ||
created: Math.floor(Date.now() / 1000) | ||
} | ||
} |
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,11 @@ | ||
import Swagger from '@elysiajs/swagger' | ||
|
||
export const swagger = Swagger({ | ||
documentation: { | ||
info: { | ||
title: 'Ava', | ||
description: 'Ava API', | ||
version: '0.0.0' | ||
} | ||
} | ||
}) |
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,8 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"paths": { | ||
"~/*": ["./src/*"], | ||
} | ||
} | ||
} |
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