diff --git a/templates/neo4j-semantic-layer/ingest.py b/templates/neo4j-semantic-layer/ingest.py index ab88221252052..77a6db92da76c 100644 --- a/templates/neo4j-semantic-layer/ingest.py +++ b/templates/neo4j-semantic-layer/ingest.py @@ -12,7 +12,8 @@ # Import movie information movies_query = """ -LOAD CSV WITH HEADERS FROM 'https://raw.githubusercontent.com/tomasonjo/blog-datasets/main/movies/movies.csv' AS row +LOAD CSV WITH HEADERS FROM +'https://raw.githubusercontent.com/tomasonjo/blog-datasets/main/movies/movies.csv' AS row CALL { WITH row MERGE (m:Movie {id:row.movieId}) @@ -35,7 +36,8 @@ # Import rating information rating_query = """ -LOAD CSV WITH HEADERS FROM 'https://raw.githubusercontent.com/tomasonjo/blog-datasets/main/movies/ratings.csv' AS row +LOAD CSV WITH HEADERS FROM +'https://raw.githubusercontent.com/tomasonjo/blog-datasets/main/movies/ratings.csv' AS row CALL { WITH row MATCH (m:Movie {id:row.movieId}) diff --git a/templates/neo4j-semantic-layer/neo4j_semantic_layer/agent.py b/templates/neo4j-semantic-layer/neo4j_semantic_layer/agent.py index 7a22bd2de2c5a..20556276fca85 100644 --- a/templates/neo4j-semantic-layer/neo4j_semantic_layer/agent.py +++ b/templates/neo4j-semantic-layer/neo4j_semantic_layer/agent.py @@ -22,9 +22,10 @@ [ ( "system", - "You are a helpful assistant that finds information about movies and recommends them. " - "If tools require follow up questions, make sure to ask the user for clarification. " - "Make sure to include any available options that need to be clarified in the follow up questions", + "You are a helpful assistant that finds information about movies " + " and recommends them. If tools require follow up questions, " + "make sure to ask the user for clarification. Make sure to include " + "any available options that need to be clarified in the follow up questions", ), MessagesPlaceholder(variable_name="chat_history"), ("user", "{input}"), diff --git a/templates/neo4j-semantic-layer/neo4j_semantic_layer/information_tool.py b/templates/neo4j-semantic-layer/neo4j_semantic_layer/information_tool.py index b136bac08bdd6..03fb550572f3f 100644 --- a/templates/neo4j-semantic-layer/neo4j_semantic_layer/information_tool.py +++ b/templates/neo4j-semantic-layer/neo4j_semantic_layer/information_tool.py @@ -18,7 +18,8 @@ WITH m, type(r) as type, collect(coalesce(t.name, t.title)) as names WITH m, type+": "+reduce(s="", n IN names | s + n + ", ") as types WITH m, collect(types) as contexts -WITH m, "type:" + labels(m)[0] + "\ntitle: "+ coalesce(m.title, m.name) + "\nyear: "+coalesce(m.released,"") +"\n" + +WITH m, "type:" + labels(m)[0] + "\ntitle: "+ coalesce(m.title, m.name) + + "\nyear: "+coalesce(m.released,"") +"\n" + reduce(s="", c in contexts | s + substring(c, 0, size(c)-2) +"\n") as context RETURN context LIMIT 1 """ @@ -30,7 +31,10 @@ def get_information(entity: str, type: str) -> str: return "No information was found about the movie or person in the database" elif len(candidates) > 1: newline = "\n" - return f"Need additional information, which of these did you mean: {newline + newline.join(str(d) for d in candidates)}" + return ( + "Need additional information, which of these " + f"did you mean: {newline + newline.join(str(d) for d in candidates)}" + ) data = graph.query( description_query, params={"candidate": candidates[0]["candidate"]} ) diff --git a/templates/neo4j-semantic-layer/neo4j_semantic_layer/memory_tool.py b/templates/neo4j-semantic-layer/neo4j_semantic_layer/memory_tool.py index 3dc70236e00ec..784ba8f432515 100644 --- a/templates/neo4j-semantic-layer/neo4j_semantic_layer/memory_tool.py +++ b/templates/neo4j-semantic-layer/neo4j_semantic_layer/memory_tool.py @@ -41,7 +41,10 @@ def store_movie_rating(movie: str, rating: int): class MemoryInput(BaseModel): movie: str = Field(description="movie the user liked") rating: int = Field( - description="Rating from 1 to 5, where one represents heavy dislike and 5 represent the user loved the movie" + description=( + "Rating from 1 to 5, where one represents heavy dislike " + "and 5 represent the user loved the movie" + ) ) diff --git a/templates/neo4j-semantic-layer/neo4j_semantic_layer/recommendation_tool.py b/templates/neo4j-semantic-layer/neo4j_semantic_layer/recommendation_tool.py index 3afb6c8100a33..a1a1fe28103b0 100644 --- a/templates/neo4j-semantic-layer/neo4j_semantic_layer/recommendation_tool.py +++ b/templates/neo4j-semantic-layer/neo4j_semantic_layer/recommendation_tool.py @@ -55,7 +55,8 @@ def recommendation_query_movie(genre: bool) -> str: def recommend_movie(movie: Optional[str] = None, genre: Optional[str] = None) -> str: """ - Recommends movies based on user's history and preference for a specific movie and/or genre. + Recommends movies based on user's history and preference + for a specific movie and/or genre. Returns: str: A string containing a list of recommended movies, or an error message. """ @@ -66,14 +67,14 @@ def recommend_movie(movie: Optional[str] = None, genre: Optional[str] = None) -> response = graph.query(recommendation_query_db_history, params) try: return ", ".join([el["movie"] for el in response]) - except: + except Exception: return "Can you tell us about some of the movies you liked?" if not movie and genre: # Recommend top voted movies in the genre the user haven't seen before response = graph.query(recommendation_query_genre, params) try: return ", ".join([el["movie"] for el in response]) - except: + except Exception: return "Something went wrong" candidates = get_candidates(movie, "movie") @@ -84,7 +85,7 @@ def recommend_movie(movie: Optional[str] = None, genre: Optional[str] = None) -> response = graph.query(query, params) try: return ", ".join([el["movie"] for el in response]) - except: + except Exception: return "Something went wrong" @@ -114,7 +115,9 @@ def recommend_movie(movie: Optional[str] = None, genre: Optional[str] = None) -> class RecommenderInput(BaseModel): movie: Optional[str] = Field(description="movie used for recommendation") genre: Optional[str] = Field( - description=f"genre used for recommendation. Available options are: {all_genres}" + description=( + "genre used for recommendation. Available options are:" f"{all_genres}" + ) ) diff --git a/templates/neo4j-semantic-layer/neo4j_semantic_layer/utils.py b/templates/neo4j-semantic-layer/neo4j_semantic_layer/utils.py index 35beaa44c3d2d..adbd4d7a7ff7b 100644 --- a/templates/neo4j-semantic-layer/neo4j_semantic_layer/utils.py +++ b/templates/neo4j-semantic-layer/neo4j_semantic_layer/utils.py @@ -34,7 +34,8 @@ def generate_full_text_query(input: str) -> str: candidate_query = """ CALL db.index.fulltext.queryNodes($index, $fulltextQuery, {limit: $limit}) YIELD node -RETURN coalesce(node.name, node.title) AS candidate, [el in labels(node) WHERE el IN ['Person', 'Movie'] | el][0] AS label +RETURN coalesce(node.name, node.title) AS candidate, + [el in labels(node) WHERE el IN ['Person', 'Movie'] | el][0] AS label """