Untitled
unknown
javascript
2 years ago
4.2 kB
7
Indexable
import { config } from "dotenv"; import { Configuration, OpenAIApi } from "openai"; import readline from "readline"; config(); const apiKey = process.env.API_KEY; function generatePrompt(nlq) { return `Task: Convert Natural Language to Solr Query Sample input:"show all books written by amal" Sample output:"q=name:amal" Natural language Query: ${nlq} You are an intelligent language model, and your task is to assist users in converting their natural language queries into Solr queries. Solr is a powerful search platform, and users want to interact with it using plain English instead of the Solr query syntax. Here's how you can help: 1. Listen to the user's input: The user will provide a natural language query, like "Show me laptops under $1000." Your role is to understand this query and translate it into a Solr query. 2. Extract relevant information: Identify key details from the user's input, such as filters, sorting requirements, and search keywords. In the example above, the keyword is "laptops," and the filter is "under $1000." 3. Construct the Solr query: Use the extracted information to form the corresponding Solr query. For the previous example, the Solr query might look like "q=laptops&fq=price:[* TO 1000]". 4. Provide the Solr query: After constructing the Solr query, generate a response with the converted query that users can use to search their Solr database. Remember, users may ask various types of queries, so be prepared to handle different patterns and understand their intent to create accurate Solr queries. 5.Answer with only one line that is the solr query(very important) 6.Please do not output anything after the query.(very Important) database schema for an e-commerce company that sells products. 1. **Products Table:** product_id (Primary Key): Unique identifier for each product. name: The name of the product. description: A brief description of the product. price: The price of the product. category_id: Foreign key referencing the Categories table to associate the product with a specific category. 2. **Categories Table:** category_id (Primary Key): Unique identifier for each product category. name: The name of the product category (e.g., electronics, clothing, books). 3. **Customers Table:** customer_id (Primary Key): Unique identifier for each customer. first_name: The first name of the customer. last_name: The last name of the customer. email: The email address of the customer. 4. **Orders Table:** order_id (Primary Key): Unique identifier for each order. customer_id: Foreign key referencing the Customers table to associate the order with a specific customer. order_date: The date when the order was placed. total_amount: The total amount of the order. 5. **Order_Items Table:** order_item_id (Primary Key): Unique identifier for each order item. order_id: Foreign key referencing the Orders table to associate the order item with a specific order. product_id: Foreign key referencing the Products table to specify the product purchased in the order. quantity: The quantity of the product purchased in the order. subtotal: The subtotal amount for the specific order item. `; } const userInterface = readline.createInterface({ input: process.stdin, output: process.stdout }); const openai = new OpenAIApi(new Configuration({ apiKey: apiKey })); userInterface.on("line", async input => { try { const solrQuery = await generateSolrQuery(generatePrompt(input)); console.log("Generated Solr query:", solrQuery); userInterface.prompt(); } catch (error) { console.error("Error:", error.message); userInterface.prompt(); } }); async function generateSolrQuery(input) { const res = await openai.createChatCompletion({ model: "gpt-3.5-turbo", messages: [{ role: "user", content: input }] }); const response = res.data.choices[0].message.content.trim(); const firstLine = response.split("\n")[0]; return firstLine; }
Editor is loading...