import "jsr:@std/dotenv@0.225/load"; import { GoogleGenerativeAI } from "npm:@google/generative-ai@0.21.0"; import { ChromaClient, GoogleGenerativeAiEmbeddingFunction, } from "npm:chromadb@1"; const DOCUMENT1 = "Operating the Climate Control System Your Googlecar has a climate control system that allows you to adjust the temperature and airflow in the car. To operate the climate control system, use the buttons and knobs located on the center console. Temperature: The temperature knob controls the temperature inside the car. Turn the knob clockwise to increase the temperature or counterclockwise to decrease the temperature. Airflow: The airflow knob controls the amount of airflow inside the car. Turn the knob clockwise to increase the airflow or counterclockwise to decrease the airflow. Fan speed: The fan speed knob controls the speed of the fan. Turn the knob clockwise to increase the fan speed or counterclockwise to decrease the fan speed. Mode: The mode button allows you to select the desired mode. The available modes are: Auto: The car will automatically adjust the temperature and airflow to maintain a comfortable level. Cool: The car will blow cool air into the car. Heat: The car will blow warm air into the car. Defrost: The car will blow warm air onto the windshield to defrost it."; const DOCUMENT2 = 'Your Googlecar has a large touchscreen display that provides access to a variety of features, including navigation, entertainment, and climate control. To use the touchscreen display, simply touch the desired icon. For example, you can touch the "Navigation" icon to get directions to your destination or touch the "Music" icon to play your favorite songs.'; const DOCUMENT3 = "Shifting Gears Your Googlecar has an automatic transmission. To shift gears, simply move the shift lever to the desired position. Park: This position is used when you are parked. The wheels are locked and the car cannot move. Reverse: This position is used to back up. Neutral: This position is used when you are stopped at a light or in traffic. The car is not in gear and will not move unless you press the gas pedal. Drive: This position is used to drive forward. Low: This position is used for driving in snow or other slippery conditions."; const documents = [DOCUMENT1, DOCUMENT2, DOCUMENT3]; const DB_NAME = "googlecardb"; const client = new ChromaClient(); const embeddingFunction = new GoogleGenerativeAiEmbeddingFunction({ googleApiKey: Deno.env.get("GOOGLE_API_KEY")!, model: "models/text-embedding-004", taskType: "RETRIEVAL_DOCUMENT", }); const db = await client.getOrCreateCollection({ name: "googlecardb", embeddingFunction, }); await db.upsert({ documents, ids: documents.map((_, i) => `${i}`), }); // Search the Chroma DB using the specified query. // @ts-ignore workaround embeddingFunction.taskType = "RETRIEVAL_QUERY"; const query = "How do you use the touchscreen to play music?"; const result = await db.query({ queryTexts: [query], nResults: 1, }); const [[passage]] = result.documents; console.log(passage); const passageOneline = passage!.replace("\n", " "); const queryOneline = query.replace("\n", " "); const prompt = `You are a helpful and informative bot that answers questions using text from the reference passage included below. Be sure to respond in a complete sentence, being comprehensive, including all relevant background information. However, you are talking to a non-technical audience, so be sure to break down complicated concepts and strike a friendly and conversational tone. If the passage is irrelevant to the answer, you may ignore it. QUESTION: ${queryOneline} PASSAGE: ${passageOneline}`; const model = new GoogleGenerativeAI(Deno.env.get("GOOGLE_API_KEY")!) .getGenerativeModel({ model: "gemini-2.0-flash", generationConfig: { temperature: 1.0, topK: 64, topP: 0.95, }, }); const answer = await model.generateContent(prompt); console.log(answer.response.text());