Skip to content

Commit

Permalink
Migrate Functions to Astro Endpoints. Use GPT-4o (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
aantipov authored Aug 7, 2024
1 parent 4f6491e commit eb4ea0b
Show file tree
Hide file tree
Showing 56 changed files with 7,342 additions and 5,611 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ jobs:
uses: actions/checkout@v3

- uses: pnpm/action-setup@v2
with:
version: 8

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 22.4
cache: 'pnpm'

- name: Install dependencies
Expand Down
15 changes: 10 additions & 5 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ export default defineConfig({
site: 'https://moiva.io/',
integrations: [vue({ appEntrypoint: '/src/main' }), tailwind()],
build: { inlineStylesheets: 'auto' },
// vite: {
// build: { minify: false },
// resolve: { alias: { '@': '/src' } },
// },
vite: {
// build: { minify: false },
// resolve: { alias: { '@': '/src' } },
ssr: { external: ['node:buffer'], noExternal: ['path-to-regexp'] },
},
output: 'hybrid',
adapter: cloudflare({ mode: 'directory' }),
adapter: cloudflare({
platformProxy: {
enabled: true,
},
}),
});
2 changes: 1 addition & 1 deletion functions-helpers/fetchPackageAIAlternatives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function fetchPkgAIAlternatives(
try {
response = await openai.chat.completions.create({
messages: getMessages(pkgName),
model: 'gpt-4-1106-preview',
model: 'gpt-4o',
response_format: { type: 'json_object' },
temperature: 0,
max_tokens: 1200,
Expand Down
4 changes: 2 additions & 2 deletions functions-helpers/fetchPackageAIInfo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
ChatCompletionRequestMessage,
type ChatCompletionRequestMessage,
Configuration,
OpenAIApi,
ResponseTypes,
type ResponseTypes,
} from 'openai-edge';
import type { AiResponseT } from '@/shared-types';

Expand Down
21 changes: 11 additions & 10 deletions functions-helpers/setAiCompareInfo.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { KVNamespace } from '@cloudflare/workers-types/experimental';
import {
ChatCompletionRequestMessage,
type ChatCompletionRequestMessage,
Configuration,
OpenAIApi,
ResponseTypes,
type ResponseTypes,
} from 'openai-edge';
import type { KvAiCompareT, AiCompareResponseT } from '@/shared-types';

Expand Down Expand Up @@ -45,7 +45,7 @@ export async function setAICompareInfo(
pkgName1: string,
pkgName2: string,
KV: KVNamespace,
openAiAPIKey: string
openAiAPIKey: string,
) {
const version = 1.0;
const model = 'gpt-3.5-turbo';
Expand All @@ -61,19 +61,19 @@ export async function setAICompareInfo(
});
if (!response.ok) {
throw new Error(
`Network response was not ok: status ${response.status}, statusText ${response.statusText}`
`Network response was not ok: status ${response.status}, statusText ${response.statusText}`,
);
}
data = (await response.json()) as ResponseTypes['createChatCompletion'];
} catch (error: any) {
throw new Error(
`[setAICompareInfo] error fetching AI data: ${error.message}`
`[setAICompareInfo] error fetching AI data: ${error.message}`,
);
}

if (data.choices[0].finish_reason !== 'stop') {
throw new Error(
`[setAICompareInfo] AI didn't finish generating. Finish reason: ${data.choices[0].finish_reason}`
`[setAICompareInfo] AI didn't finish generating. Finish reason: ${data.choices[0].finish_reason}`,
);
}

Expand All @@ -90,7 +90,7 @@ export async function setAICompareInfo(
}
if ('notFound' in content && content.notFound !== true) {
throw new Error(
'[setAICompareInfo] AI reply doesnt match schema - ' + contentRaw
'[setAICompareInfo] AI reply doesnt match schema - ' + contentRaw,
);
}
if (
Expand All @@ -99,11 +99,12 @@ export async function setAICompareInfo(
content.length === 0 ||
!content.every(
(item) =>
typeof item.title === 'string' && typeof item.description === 'string'
typeof item.title === 'string' &&
typeof item.description === 'string',
))
) {
throw new Error(
'[setAICompareInfo] AI reply doesnt match schema - ' + contentRaw
'[setAICompareInfo] AI reply doesnt match schema - ' + contentRaw,
);
}
try {
Expand All @@ -125,7 +126,7 @@ export async function setAICompareInfo(

function getMessages(
pkgName1: string,
pkgName2: string
pkgName2: string,
): ChatCompletionRequestMessage[] {
return [
{
Expand Down
9 changes: 0 additions & 9 deletions functions/tsconfig.json

This file was deleted.

9 changes: 0 additions & 9 deletions functions/worker-configuration.d.ts

This file was deleted.

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "moiva",
"version": "1.0.3",
"private": true,
"packageManager": "pnpm@8.11.0",
"packageManager": "pnpm@9.0.6",
"scripts": {
"dev": "echo 'To be implemented. For now, use `pr \"/^preview:pure$|^watch$/\"`'",
"watch": "nodemon -e astro,ts,vue,json --watch ./src --exec 'astro build'",
Expand All @@ -26,16 +26,17 @@
"blog:ci": "pnpm run blog:build && pnpm run blog:move"
},
"dependencies": {
"@astrojs/cloudflare": "^6.6.2",
"@astrojs/tailwind": "^4.0.0",
"@astrojs/vue": "^2.2.1",
"@astrojs/check": "^0.9.1",
"@astrojs/cloudflare": "^11.0.4",
"@astrojs/tailwind": "^5.1.0",
"@astrojs/vue": "^4.5.0",
"@nanostores/vue": "^0.10.0",
"@sentry/node": "^7.60.0",
"@sentry/tracing": "^7.60.0",
"@sentry/vue": "^7.60.0",
"@tailwindcss/typography": "^0.5.9",
"@vueuse/core": "^10.2.1",
"astro": "^2.9.2",
"astro": "^4.13.1",
"autocompleter": "^6.1.3",
"axios": "^1.4.0",
"chart.js": "^4.3.0",
Expand All @@ -45,7 +46,7 @@
"google-trends-api": "^4.9.2",
"nanoid": "^4.0.2",
"nanostores": "^0.9.3",
"openai": "^4.20.1",
"openai": "^4.54.0",
"openai-edge": "^1.2.2",
"ramda": "^0.29.0",
"reading-time": "^1.5.0",
Expand All @@ -56,7 +57,7 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230717.1",
"@cloudflare/workers-types": "^4.20240729.0",
"@netlify/functions": "^0.10.0",
"@types/node": "^20.4.4",
"@types/ramda": "^0.29.3",
Expand All @@ -77,7 +78,7 @@
"vitest": "^0.33.0",
"vue-eslint-parser": "^9.3.1",
"vue-tsc": "^1.8.6",
"wrangler": "^3.3.0",
"wrangler": "^3.68.0",
"zx": "^7.2.3"
},
"engineStrict": true,
Expand Down
Loading

0 comments on commit eb4ea0b

Please sign in to comment.