Skip to content

Commit b4dc01c

Browse files
Express application
1 parent 5a2d90c commit b4dc01c

31 files changed

+1292
-4
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ testem.log
3838
.DS_Store
3939
Thumbs.db
4040

41-
.nx/cache
41+
.nx/cache
42+
43+
# Env Files
44+
.env.*
45+
*.env
46+
!example.env

example.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
WEATHER_API_KEY=
2+
OPENAI_API_KEY=
3+
4+
5+
VAPI_BASE_URL=https://api.vapi.ai
6+
VAPI_API_KEY=

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
},
1010
"private": true,
1111
"dependencies": {
12-
"axios": "^1.6.0",
12+
"axios": "^1.6.7",
13+
"cors": "^2.8.5",
1314
"express": "~4.18.1",
1415
"llamaindex": "^0.1.9",
1516
"openai": "^4.26.1",

pnpm-lock.yaml

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config/env.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const envConfig = {
2+
weather: {
3+
baseUrl:
4+
process.env.WEATHER_BASE_URL ?? `https://api.openweathermap.org/data/2.5`,
5+
apiKey: process.env.WEATHER_API_KEY ?? ``,
6+
},
7+
openai: {
8+
apiKey: process.env.OPENAI_API_KEY ?? ``,
9+
},
10+
vapi: {
11+
baseUrl: process.env.VAPI_BASE_URL ?? "https://api.vapi.ai",
12+
apiKey: process.env.VAPI_API_KEY ?? "",
13+
},
14+
};

src/data/Activities.md

Lines changed: 158 additions & 0 deletions
Large diffs are not rendered by default.

src/data/Characters.md

Lines changed: 150 additions & 0 deletions
Large diffs are not rendered by default.

src/functions/fetchKeyword.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import axios from 'axios';
2+
3+
interface KeywordParams {
4+
keyword: string;
5+
topic?: string;
6+
}
7+
8+
export const findKeywords = (opts: KeywordParams) => {
9+
return axios
10+
.get(`https://api.datamuse.com/words`, {
11+
params: {
12+
ml: opts.keyword,
13+
topics: opts.topic,
14+
},
15+
})
16+
.then(
17+
(response) =>
18+
response.data
19+
.map((item) => item.word)
20+
.slice(0, Math.min(response.data.length, 10)) ?? []
21+
);
22+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { SimpleDirectoryReader, VectorStoreIndex } from 'llamaindex';
2+
import path from 'path';
3+
4+
interface GetCharacterInspirationParams {
5+
inspiration: string;
6+
}
7+
8+
export const getCharacterInspiration = async ({
9+
inspiration,
10+
}: GetCharacterInspirationParams) => {
11+
const fallbackResponse = {
12+
result:
13+
'Sorry, I am dealing with a technical issue at the moment, perhaps because of heightened user traffic. Come back later and we can try this again. Apologies for that.',
14+
};
15+
if (inspiration) {
16+
try {
17+
const documents = await new SimpleDirectoryReader().loadData({
18+
directoryPath: path.join(__dirname, '../data'),
19+
});
20+
21+
const index = await VectorStoreIndex.fromDocuments(documents);
22+
23+
const queryEngine = index.asQueryEngine();
24+
const response = await queryEngine.query({ query: inspiration });
25+
26+
return { result: response.response, forwardToClientEnabled: true };
27+
} catch (error) {
28+
console.log('error', error);
29+
return fallbackResponse;
30+
}
31+
} else {
32+
return fallbackResponse;
33+
}
34+
};

src/functions/getRandomName.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import axios from 'axios';
2+
3+
const nats = [
4+
'AU',
5+
'CA',
6+
'FR',
7+
'IN',
8+
'IR',
9+
'MX',
10+
'NL',
11+
'NO',
12+
'NZ',
13+
'RS',
14+
'TR',
15+
'US',
16+
];
17+
18+
interface NameParams {
19+
gender?: 'male' | 'female';
20+
nat?: (typeof nats)[number];
21+
}
22+
23+
export const getRandomName = async (params: NameParams) => {
24+
const nat =
25+
params.nat && !nats.includes(params.nat.toUpperCase())
26+
? nats[Math.floor(Math.random() * nats.length)]
27+
: params.nat ?? '';
28+
29+
try {
30+
const results = await axios.get(`https://randomuser.me/api/`, {
31+
params: {
32+
...params,
33+
nat,
34+
},
35+
});
36+
37+
const name = results.data.results[0].name;
38+
console.log('results', params, name);
39+
return {
40+
result: name.first + ' ' + name.last,
41+
};
42+
} catch (err) {
43+
throw new Error('Error fetching random name');
44+
}
45+
};

0 commit comments

Comments
 (0)