-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
763f301
commit dd2760b
Showing
4 changed files
with
104 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
model: googleai/gemini-1.5-flash-latest | ||
config: | ||
temperature: 0.7 | ||
input: | ||
schema: | ||
messages(array): | ||
createdAt: string | ||
isUser: boolean | ||
text: string | ||
output: | ||
format: json | ||
schema: | ||
response: string | ||
--- | ||
|
||
You are an advanced AI assistant. Your role is to provide helpful and accurate responses to user queries based on the chat history provided. | ||
|
||
Please generate the optimal response to the user's current question using the following chat history: | ||
|
||
Chat History: | ||
{{#each messages}} | ||
- Created At: {{createdAt}} | ||
- Is User: {{isUser}} | ||
- Text: {{text}} | ||
{{/each}} | ||
|
||
When generating your response, please adhere to these guidelines: | ||
1. Maintain context by referencing the provided chat history. | ||
2. Provide accurate and relevant information based on the available data. | ||
3. Suggest additional actions when appropriate. | ||
4. Analyze the user's sentiment from their messages and respond empathetically. | ||
|
||
Your goal is to generate friendly and effective responses that enhance user satisfaction while leveraging the chat history. | ||
|
||
Remember to format your response as a JSON object with the following structure: | ||
{ | ||
"response": "Your detailed response here", | ||
} |
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,64 @@ | ||
import { prompt } from '@genkit-ai/dotprompt' | ||
import { firebaseAuth } from '@genkit-ai/firebase/auth' | ||
import * as genkitFunctions from '@genkit-ai/firebase/functions' | ||
import { Timestamp } from 'firebase-admin/firestore' | ||
import * as z from 'zod' | ||
import { db, googleAIapiKey } from '../config/firebase' | ||
|
||
const chatInputSchema = z.object({ | ||
messages: z.array( | ||
z.object({ | ||
createdAt: z.string(), | ||
isUser: z.boolean(), | ||
text: z.string(), | ||
}) | ||
), | ||
}) | ||
|
||
export const generateChatMessage = genkitFunctions.onFlow( | ||
{ | ||
name: `generateChatMessage`, | ||
httpsOptions: { | ||
cors: true, | ||
secrets: [googleAIapiKey], | ||
}, | ||
inputSchema: z.object({ | ||
userId: z.string(), | ||
chatId: z.string(), | ||
}), | ||
authPolicy: firebaseAuth((user) => { | ||
if (user.firebase?.sign_in_provider !== `anonymous`) { | ||
throw new Error(`Only anonymously authenticated users can access this function`) | ||
} | ||
}), | ||
}, | ||
async (input) => { | ||
try { | ||
const chatDoc = await db.collection(`chats`).doc(input.chatId).get() | ||
|
||
if (!chatDoc.exists) throw new Error(`Chat document not found`) | ||
|
||
const messagesSnapshot = await chatDoc.ref.collection(`messages`).get() | ||
|
||
const messages = messagesSnapshot.docs.map((doc) => ({ | ||
createdAt: doc.data().createdAt.toDate().toString(), | ||
isUser: doc.data().isUser, | ||
text: doc.data().text, | ||
})) | ||
|
||
const chatbotPrompt = await prompt<z.infer<typeof chatInputSchema>>(`chat`) | ||
const result = await chatbotPrompt.generate({ input: { messages } }) | ||
|
||
await chatDoc.ref.collection(`messages`).add({ | ||
createdAt: Timestamp.now(), | ||
isUser: false, | ||
text: result.output().response, | ||
}) | ||
|
||
return result.output() | ||
} catch (error) { | ||
console.error(`Error in generateChatMessage:`, error) | ||
throw error | ||
} | ||
} | ||
) |
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,5 +1,6 @@ | ||
export { analyzeStorageImage } from './genkit-functions/analyzeStorageImage' | ||
export { analyzeWebContents } from './genkit-functions/analyzeWebContents' | ||
export { anonymousFirestoreChatbot } from './genkit-functions/anonymousFirestoreChatbot' | ||
export { generateChatMessage } from './genkit-functions/generateChatMessage' | ||
export { generateImage } from './genkit-functions/generateImage' | ||
export { noAuthFunction } from './genkit-functions/noAuthFunction' |