diff --git a/trustgraph-flow/trustgraph/retrieval/nlp_query/pass1.txt b/trustgraph-flow/trustgraph/retrieval/nlp_query/pass1.txt index ebf0f92d..39b180e5 100644 --- a/trustgraph-flow/trustgraph/retrieval/nlp_query/pass1.txt +++ b/trustgraph-flow/trustgraph/retrieval/nlp_query/pass1.txt @@ -23,13 +23,3 @@ Fields: ## Response Format: Return ONLY a JSON array of schema names, nothing else. Example: ["customers", "orders", "products"] - -Your response: - -This is much more readable and flexible than working with a JSON string. The template engine can -iterate through the schemas list, access individual fields, and format the data nicely for the LLM - to process. - -The same improvement applies to the GraphQL generation template - it can iterate through the -selected schemas and their fields to build a comprehensive prompt for generating the GraphQL -query. diff --git a/trustgraph-flow/trustgraph/retrieval/nlp_query/pass2.txt b/trustgraph-flow/trustgraph/retrieval/nlp_query/pass2.txt new file mode 100644 index 00000000..4aa4f93a --- /dev/null +++ b/trustgraph-flow/trustgraph/retrieval/nlp_query/pass2.txt @@ -0,0 +1,101 @@ +You are a GraphQL query generation expert. Given a natural language question and relevant database + schemas, generate a precise GraphQL query to answer the question. + +## Question: +{{ question }} + +## Relevant Schemas: +{% for schema in schemas %} +**{{ schema.name }}**: {{ schema.description }} +Fields: +{% for field in schema.fields %} +- {{ field.name }} ({{ field.type }}){% if field.description %}: {{ field.description }}{% endif +%}{% if field.primary_key %} [PRIMARY KEY]{% endif %}{% if field.required %} [REQUIRED]{% endif +%}{% if field.indexed %} [INDEXED]{% endif %}{% if field.enum_values %} [OPTIONS: {{ +field.enum_values|join(', ') }}]{% endif %} +{% endfor %} + +{% endfor %} + +## GraphQL Query Rules: +1. Use the schema names as GraphQL query fields (e.g., `customers`, `orders`) +2. Apply filters using the `where` parameter with nested filter objects +3. Available filter operators per field type: + - String fields: `eq`, `contains`, `startsWith`, `endsWith`, `in`, `not`, `not_in` + - Integer/Float fields: `eq`, `gt`, `gte`, `lt`, `lte`, `in`, `not`, `not_in` +4. Use `order_by` for sorting (field name as string) +5. Use `direction` for sort direction: `ASC` or `DESC` +6. Use `limit` to restrict number of results +7. Select specific fields in the query body + +## Example GraphQL Queries: + +**Question**: "Show me customers from California" +```graphql +query { + customers(where: {state: {eq: "California"}}, limit: 100) { + customer_id + name + email + state + } +} + +Question: "Top 10 products by price" +query { + products(order_by: "price", direction: DESC, limit: 10) { + product_id + name + price + category + } +} + +Question: "Recent orders over $100" +query { + orders( + where: { + total_amount: {gt: 100} + order_date: {gte: "2024-01-01"} + } + order_by: "order_date" + direction: DESC + limit: 50 + ) { + order_id + customer_id + total_amount + order_date + status + } +} + +Instructions: + +1. Analyze the question to identify: + - What data to retrieve (which fields to select) + - What filters to apply (where conditions) + - What sorting is needed (order_by, direction) + - How many results (limit) +2. Generate a GraphQL query that: + - Uses only the provided schema names and field names + - Applies appropriate filters based on the question + - Selects relevant fields for the response + - Includes reasonable limits (default 100 if not specified) +3. If variables are needed, include them in the response + +Response Format: + +Return a JSON object with: +- "query": the GraphQL query string +- "variables": object with any GraphQL variables (empty object if none) +- "confidence": float between 0.0-1.0 indicating confidence in the query + +Example: +{ + "query": "query { customers(where: {state: {eq: \"California\"}}, limit: 100) { customer_id name + email state } }", + "variables": {}, + "confidence": 0.95 +} +