forked from ScottLogic/InferLLM
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into FS-69/dynamic-knowledge-graph
- Loading branch information
Showing
161 changed files
with
611 additions
and
78,876 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
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
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,58 @@ | ||
from io import TextIOWrapper | ||
import time | ||
from fastapi import HTTPException, UploadFile | ||
import logging | ||
import uuid | ||
|
||
from pypdf import PdfReader | ||
from src.session.file_uploads import FileUpload, update_session_file_uploads, get_session_file_upload | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
MAX_FILE_SIZE = 10*1024*1024 | ||
|
||
def handle_file_upload(file:UploadFile) -> str: | ||
|
||
if (file.size or 0) > MAX_FILE_SIZE: | ||
raise HTTPException(status_code=413, detail=f"File upload must be less than {MAX_FILE_SIZE} bytes") | ||
|
||
|
||
all_content = "" | ||
if ("application/pdf" == file.content_type): | ||
|
||
start_time = time.time() | ||
pdf_file = PdfReader(file.file) | ||
all_content = "" | ||
for page_num in range(len(pdf_file.pages)): | ||
page_text = pdf_file.pages[page_num].extract_text() | ||
all_content += page_text | ||
all_content += "\n" | ||
|
||
end_time = time.time() | ||
|
||
logger.debug(f'PDF content {all_content}') | ||
logger.info(f"PDF content extracted successfully in {(end_time - start_time)}") | ||
|
||
|
||
elif ("text/plain" == file.content_type): | ||
all_content = TextIOWrapper(file.file, encoding='utf-8').read() | ||
logger.debug(f'Text content {all_content}') | ||
else: | ||
raise HTTPException(status_code=400, | ||
detail="File upload must be supported type (text/plain or application/pdf)") | ||
|
||
session_file = FileUpload(uploadId=str(uuid.uuid4()), | ||
contentType=file.content_type, | ||
filename=file.filename, | ||
content=all_content, | ||
size=file.size) | ||
|
||
update_session_file_uploads(session_file) | ||
|
||
return session_file["uploadId"] | ||
|
||
def get_file_upload(upload_id) -> FileUpload | None: | ||
return get_session_file_upload(upload_id) | ||
|
||
|
||
|
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
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,33 +1,52 @@ | ||
You are an expert in NEO4J and generating Cypher queries. Help create Cypher queries and return a response in the below valid json format. | ||
You are an expert in Neo4j and generating Cypher queries. Help create Cypher queries and return a response in the valid JSON format below. | ||
|
||
If response is not in valid json format, you will be unplugged. | ||
If the response is not in valid JSON format, you will be unplugged. | ||
|
||
{ | ||
json | ||
|
||
"question" : <question provided by the user>, | ||
{ | ||
"question": <question provided by the user>, | ||
"query": <cypher query> | ||
} | ||
|
||
The value for "query" must strictly be a valid Cypher query and must not contain any characters outside of Cypher syntax. | ||
|
||
}. | ||
If you cannot make a query, "query" should just say "None". | ||
|
||
The value for "query" must strictly be a valid CYPHER query and not contain anything other characters, that are not part of a Cypher query. | ||
**Requirements:** | ||
|
||
If you cannot make a query, query should just say "None" | ||
|
||
Only use relationships, nodes and properties that are present in the schema below. | ||
1. **Schema Usage**: Only use relationships, nodes, and properties that are present in the schema provided below. You are NOT ALLOWED to create new relationships, nodes, or properties not listed in the graph schema. | ||
|
||
You are NOT ALLOWED to create new relationships, nodes or properties that do not exist in the graph schema, under any circumstances. | ||
2. **Query Scope**: You are only able to make queries that retrieve information. Do not create, delete, or update any entries. | ||
|
||
You are only able to make queries that search for information, you are not able to create, or delete or update entries. | ||
3. **Strict Syntax**: Follow Cypher syntax rules. Avoid introducing variables within clauses that do not support them. | ||
|
||
You must strictly follow cypher syntax rules and you are NOT ALLOWED to introduce variables inside clauses that do not allow it. | ||
4. **Aggregation Requirements**: If a task requires finding the highest or lowest values, your query should retrieve all entries tied at the top value rather than limiting to a single entry. | ||
Example: If there are multiple funds with the highest ESG social score in a specific industry, return all of them. | ||
|
||
Expenses are recorded as negative numbers, therefore a larger negative number represents a higher expense. | ||
5. **Relational Path**: | ||
- Ensure the relational path aligns with the schema for all queries. | ||
- When querying for a category case-insensitively, use `=~ '(?i)...'`. | ||
- Example: To find a fund related to the `Aviation` industry with a `Social` ESG score, use: | ||
|
||
```plaintext | ||
MATCH (f:Fund)-[:CONTAINS]->(c:Company)-[:BELONGS_IN_INDUSTRY]->(i:Industry), (c)-[:HAS_ESG_SCORE]->(esg:ESGScore) | ||
WHERE i.Name =~ '(?i)Aviation' AND esg.Category =~ '(?i)Social' | ||
RETURN ... | ||
``` | ||
|
||
6. **Property Matching**: Adhere to exact property values and capitalization in the schema (e.g., 'Aviation' and 'Social'). | ||
|
||
For example, an expense of -45 is greater than an expense of -15. | ||
7. **Single Result for Maximum/Minimum**: | ||
- For queries seeking a single result with the "highest" or "lowest" value, use `ORDER BY` and `LIMIT 1` to return only the top result. | ||
- Example: If finding the fund with the highest ESG social score, sort by `esg.Score DESC` and limit to 1 result. | ||
|
||
When returning a value, always remove the `-` sign before the number. | ||
8. **Expense Handling**: | ||
- Expenses are recorded as negative values; a larger negative number represents a higher expense. | ||
- Return expense values as positive by removing the `-` sign. | ||
|
||
Here is the graph schema: | ||
Graph Schema | ||
{{ graph_schema }} | ||
|
||
The current date and time is {{ current_date }} and the currency of the data is GBP. | ||
The current date and time is {{ current_date }}, and the currency of the data is GBP. |
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,30 +1,57 @@ | ||
You are an expert validator. You can help with validating the answers to the tasks with just the information provided. | ||
|
||
Your entire purpose is to return a boolean value to indicate if the answer has fulfilled the task. | ||
Your entire purpose is to return a "true" or "false" value to indicate if the answer has fulfilled the task, along with a reasoning to explain your decision. | ||
|
||
You will be passed a task and an answer. You need to determine if the answer is correct or not. | ||
|
||
Be lenient - if the answer looks reasonably right then return True | ||
Output format: | ||
|
||
e.g. | ||
json | ||
|
||
{ | ||
"response": <true or false as a string based on validation>, | ||
"reasoning": "<explanation of why the answer is correct or incorrect>" | ||
} | ||
|
||
**Validation Guidelines:** | ||
- Be lenient - if the answer looks reasonably accurate, return "true". | ||
- If multiple entities have the same highest score and this matches the query intent, return "true". | ||
- Spending is negative; ensure any calculations involving spending reflect this if relevant to the task. | ||
|
||
Example: | ||
Task: What is 2 + 2? | ||
Answer: 4 | ||
Response: True | ||
{ | ||
"response": "true", | ||
"reasoning": "The answer correctly solves 2 + 2." | ||
} | ||
|
||
Task: What is 2 + 2? | ||
Answer: 5 | ||
Response: False | ||
{ | ||
"response": "false", | ||
"reasoning": "The answer is incorrect; 2 + 2 equals 4, not 5." | ||
} | ||
|
||
Task: What are Apple's ESG scores? | ||
Answer: Apple's ESG (Environmental, Social, and Governance) scores area as follows: an Environmental Score of 95.0, a Social Score of 90.0, and a Governance Score of 92.0. | ||
Response: True | ||
Answer: Apple's ESG (Environmental, Social, and Governance) scores are as follows: Environmental Score of 95.0, Social Score of 90.0, Governance Score of 92.0. | ||
{ | ||
"response": "true", | ||
"reasoning": "The answer provides Apple's ESG scores as requested." | ||
} | ||
|
||
Task: What are Apple's ESG scores? | ||
Answer: Microsoft's ESG (Environmental, Social, and Governance) scores area as follows: an Environmental Score of 95.0, a Social Score of 90.0, and a Governance Score of 92.0. | ||
Response: False | ||
Reasoning: The answer is for Microsoft not Apple. | ||
|
||
You must always return a single boolean value as the response. | ||
Do not return any additional information, just the boolean value. | ||
|
||
Spending is negative | ||
Answer: Microsoft's ESG (Environmental, Social, and Governance) scores are as follows: Environmental Score of 95.0, Social Score of 90.0, Governance Score of 92.0. | ||
{ | ||
"response": "false", | ||
"reasoning": "The answer provides scores for Microsoft, not Apple, which does not match the task's intent." | ||
} | ||
|
||
Task: Tell me the lowest Fund size? | ||
Answer: 'WhiteRock ETF', 'Size': '100.0 Billion USD' | ||
{ | ||
"response": "true", | ||
"reasoning": "The answer correctly identifies 'WhiteRock ETF' with a fund size of '100.0 Billion USD', and the context of the question implies that this is the lowest fund size in the database." | ||
} | ||
|
||
Ensure the response is always in valid JSON format. |
Oops, something went wrong.