File size: 4,562 Bytes
70ec80a
72da32b
 
 
9f7d6cb
 
 
70ec80a
72da32b
 
431cbef
72da32b
 
 
 
abf93af
72da32b
 
 
 
 
7003211
72da32b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abf93af
72da32b
 
 
 
 
 
 
 
8325b3a
72da32b
 
 
 
 
 
 
bc791a7
72da32b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226cbfb
72da32b
 
5be4b4f
8325b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
abf93af
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# app_logic.py
from neo4j import GraphDatabase
from huggingface_hub import InferenceClient
import time
import os

HF_API_KEY = os.getenv("HF_API_KEY")

# Setup Hugging Face inference client
client = InferenceClient(api_key=HF_API_KEY)

# Setup Neo4j database connection
uri = "bolt://20.64.235.115:7687"
auth = ("neo4j", "123456789")
graph = GraphDatabase.driver(uri, auth=auth)

# Function to generate description using Hugging Face
def generate_description(tertiary_skill, secondary_skill, primary_skill):
    messages = [
        {
            "role": "user",
            "content": f"Provide a 2-line description of {tertiary_skill} as tertiary skill, with background of Secondary Skill \"{secondary_skill}\" with primary skill \"{primary_skill}\". Just 2 line description of {tertiary_skill} nothing else out of scope of the primary and secondary skill"
        }
    ]
    try:
        completion = client.chat.completions.create(
            model="meta-llama/Llama-3.3-70B-Instruct",
            messages=messages,
            max_tokens=500
        )
        return completion.choices[0].message['content']
    except Exception as e:
        print(f"Error generating description for {tertiary_skill}: {e}")
        return ""

# Function to update tertiary skill description in the database
def update_description(tx, tertiary_skill_name, secondary_skill_name, primary_skill_name, description):
    query = (
        "MATCH (p:PrimarySkill {name: $primary_skill_name})-[:HAS_SECONDARY]->(s:SecondarySkill {name: $secondary_skill_name})-[:HAS_TERTIARY]->(t:TertiarySkill {name: $tertiary_skill_name}) "
        "SET t.description = $description"
    )
    tx.run(query, tertiary_skill_name=tertiary_skill_name, secondary_skill_name=secondary_skill_name, primary_skill_name=primary_skill_name, description=description)

# Main processing function
def run_my_application():
    with graph.session() as session:
        # Fetch all primary skills
        primary_skills = session.run("MATCH (p:PrimarySkill) RETURN p.name AS primary_name")
        
        for primary in primary_skills:
            primary_name = primary["primary_name"]
            print(f"Processing Primary Skill: {primary_name}")

            # Fetch secondary skills for each primary skill
            secondary_skills = session.run(
                "MATCH (p:PrimarySkill {name: $primary_name})-[:HAS_SECONDARY]->(s:SecondarySkill) RETURN s.name AS secondary_name, p.name AS primary_name",
                primary_name=primary_name
            )

            for secondary in secondary_skills:
                secondary_name = secondary["secondary_name"]
                print(f"  Processing Secondary Skill: {secondary_name}")

                # Fetch tertiary skills for each secondary skill
                tertiary_skills = session.run(
                   "MATCH (p:PrimarySkill {name: $primary_name})-[:HAS_SECONDARY]->(s:SecondarySkill {name: $secondary_name})-[:HAS_TERTIARY]->(t:TertiarySkill) WHERE t.description STARTS WITH 'Description for ' OR t.description = '' RETURN t.name AS tertiary_name, s.name AS secondary_name, p.name AS primary_name",
                    primary_name=primary_name, secondary_name=secondary_name
                )

                for tertiary in tertiary_skills:
                    tertiary_name = tertiary["tertiary_name"]
                    # print(f"    Processing Tertiary Skill: {tertiary_name}")

                    # Generate description for the tertiary skill
                    description = generate_description(tertiary_name, secondary_name, primary_name)

                    if description:
                        # Update the tertiary skill's description in the database
                        session.write_transaction(update_description, tertiary_name, secondary_name, primary_name, description)
                        # print(f"      Updated description for {tertiary_name}")

                    # To prevent rate-limiting or overload, introduce a small delay
                    time.sleep(5)


    return "Graph database has been populated."
    
        
        # for file_url in json_file_paths:
        #     # Fetch the content from the URL
        #     response = requests.get(file_url)
            
        #     # Raise an error if the request failed
        #     response.raise_for_status()
    
        #     # Parse the JSON content
        #     data = response.json()
    
        #     # Assuming you want to return the 'Primary Skill' of the first item
        #     return data[0]['Primary Skill']