Skip to content

Commit 7fc2fd4

Browse files
Merge pull request #18 from nozomi-koborinai/feature/chatbot
feat: generateChatMessage
2 parents 763f301 + dd2760b commit 7fc2fd4

File tree

4 files changed

+104
-6
lines changed

4 files changed

+104
-6
lines changed

firebase.json

-6
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,9 @@
3535
"pubsub": {
3636
"port": 8085
3737
},
38-
"storage": {
39-
"port": 9199
40-
},
4138
"eventarc": {
4239
"port": 9299
4340
},
44-
"dataconnect": {
45-
"port": 9399
46-
},
4741
"tasks": {
4842
"port": 9499
4943
},

functions/prompts/chat.prompt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
model: googleai/gemini-1.5-flash-latest
3+
config:
4+
temperature: 0.7
5+
input:
6+
schema:
7+
messages(array):
8+
createdAt: string
9+
isUser: boolean
10+
text: string
11+
output:
12+
format: json
13+
schema:
14+
response: string
15+
---
16+
17+
You are an advanced AI assistant. Your role is to provide helpful and accurate responses to user queries based on the chat history provided.
18+
19+
Please generate the optimal response to the user's current question using the following chat history:
20+
21+
Chat History:
22+
{{#each messages}}
23+
- Created At: {{createdAt}}
24+
- Is User: {{isUser}}
25+
- Text: {{text}}
26+
{{/each}}
27+
28+
When generating your response, please adhere to these guidelines:
29+
1. Maintain context by referencing the provided chat history.
30+
2. Provide accurate and relevant information based on the available data.
31+
3. Suggest additional actions when appropriate.
32+
4. Analyze the user's sentiment from their messages and respond empathetically.
33+
34+
Your goal is to generate friendly and effective responses that enhance user satisfaction while leveraging the chat history.
35+
36+
Remember to format your response as a JSON object with the following structure:
37+
{
38+
"response": "Your detailed response here",
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { prompt } from '@genkit-ai/dotprompt'
2+
import { firebaseAuth } from '@genkit-ai/firebase/auth'
3+
import * as genkitFunctions from '@genkit-ai/firebase/functions'
4+
import { Timestamp } from 'firebase-admin/firestore'
5+
import * as z from 'zod'
6+
import { db, googleAIapiKey } from '../config/firebase'
7+
8+
const chatInputSchema = z.object({
9+
messages: z.array(
10+
z.object({
11+
createdAt: z.string(),
12+
isUser: z.boolean(),
13+
text: z.string(),
14+
})
15+
),
16+
})
17+
18+
export const generateChatMessage = genkitFunctions.onFlow(
19+
{
20+
name: `generateChatMessage`,
21+
httpsOptions: {
22+
cors: true,
23+
secrets: [googleAIapiKey],
24+
},
25+
inputSchema: z.object({
26+
userId: z.string(),
27+
chatId: z.string(),
28+
}),
29+
authPolicy: firebaseAuth((user) => {
30+
if (user.firebase?.sign_in_provider !== `anonymous`) {
31+
throw new Error(`Only anonymously authenticated users can access this function`)
32+
}
33+
}),
34+
},
35+
async (input) => {
36+
try {
37+
const chatDoc = await db.collection(`chats`).doc(input.chatId).get()
38+
39+
if (!chatDoc.exists) throw new Error(`Chat document not found`)
40+
41+
const messagesSnapshot = await chatDoc.ref.collection(`messages`).get()
42+
43+
const messages = messagesSnapshot.docs.map((doc) => ({
44+
createdAt: doc.data().createdAt.toDate().toString(),
45+
isUser: doc.data().isUser,
46+
text: doc.data().text,
47+
}))
48+
49+
const chatbotPrompt = await prompt<z.infer<typeof chatInputSchema>>(`chat`)
50+
const result = await chatbotPrompt.generate({ input: { messages } })
51+
52+
await chatDoc.ref.collection(`messages`).add({
53+
createdAt: Timestamp.now(),
54+
isUser: false,
55+
text: result.output().response,
56+
})
57+
58+
return result.output()
59+
} catch (error) {
60+
console.error(`Error in generateChatMessage:`, error)
61+
throw error
62+
}
63+
}
64+
)

functions/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { analyzeStorageImage } from './genkit-functions/analyzeStorageImage'
22
export { analyzeWebContents } from './genkit-functions/analyzeWebContents'
33
export { anonymousFirestoreChatbot } from './genkit-functions/anonymousFirestoreChatbot'
4+
export { generateChatMessage } from './genkit-functions/generateChatMessage'
45
export { generateImage } from './genkit-functions/generateImage'
56
export { noAuthFunction } from './genkit-functions/noAuthFunction'

0 commit comments

Comments
 (0)