import os import streamlit as st from langchain_community.graphs import Neo4jGraph from langchain_groq import ChatGroq from langchain.chains import GraphCypherQAChain from dotenv import load_dotenv # Load environment variables load_dotenv() NEO4J_URI = os.getenv("NEO4J_URI") NEO4J_USERNAME = os.getenv("NEO4J_USERNAME") NEO4J_PASSWORD = os.getenv("NEO4J_PASSWORD") GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Initialize Neo4j graph graph = Neo4jGraph(url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD) # Load movie dataset into Neo4j moview_query = """ LOAD CSV WITH HEADERS FROM 'https://raw.githubusercontent.com/tomasonjo/blog-datasets/main/movies/movies_small.csv' as row MERGE(m:Movie{id:row.movieId}) SET m.released = date(row.released), m.title = row.title, m.imdbRating = toFloat(row.imdbRating) FOREACH (director in split(row.director, '|') | MERGE (p:Person {name:trim(director)}) MERGE (p)-[:DIRECTED]->(m)) FOREACH (actor in split(row.actors, '|') | MERGE (p:Person {name:trim(actor)}) MERGE (p)-[:ACTED_IN]->(m)) FOREACH (genre in split(row.genres, '|') | MERGE (g:Genre {name:trim(genre)}) MERGE (m)-[:IN_GENRE]->(g)) """ graph.query(moview_query) graph.refresh_schema() # Initialize LLM llm = ChatGroq(groq_api_key=GROQ_API_KEY, model_name="Gemma2-9b-It") # Create QA Chain chain = GraphCypherQAChain.from_llm(graph=graph, llm=llm, verbose=True) # Streamlit UI st.title("Movie Director Finder") query = st.text_input("Enter your query:", "Who was the director of the movie Casino") if st.button("Find Director"): response = chain.invoke({"query": query}) st.write(response)