from gliner import GLiNER #Model Initialization model = GLiNER.from_pretrained("numind/NuNerZero") #Merging and Displaying Entities # NuZero requires labels to be lower-cased! labels=[ "people", "organizations", "concepts/terms", "principles", "documents", "dates" ] labels = [l.lower() for l in labels] text = content_process entities = model.predict_entities(text, labels) entities = merge_entities(entities) for entity in entities: print(entity["text"], "=>", entity["label"])
import openai import json # Function to generate entities and relationships from the given text using OpenAI's API def generate_entities_and_relationships(text, api_key): # Set the OpenAI API key openai.api_key = api_key # Create the prompt that will be sent to the OpenAI API. # The prompt asks the model to identify entities and relationships within the provided text # and format the response in JSON format. prompt = f""" Given the following text, identify the main entities and their relationships: Text: {text} Please provide the output in the following JSON format: {{ "entities": [ {{"name": "Entity1", "type": "ersonType"}}, {{"name": "Entity2", "type": "OrganizationType"}}, ...], "relationships": [ {{"subject": "Entity1", "predicate": "works_for", "object": "Entity2"}}, {{"subject": "Entity2", "predicate": "located_in", "object": "Entity3"}}, ...]}}""" # Send the request to the OpenAI API using the 'gpt-3.5-turbo' model. # The API call is structured as a chat completion with system and user messages. response = openai.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "You are a helpful assistant that identifies entities and relationships in text."}, {"role": "user", "content": prompt} ] )# Extract and clean up the response by removing extra characters or code format markers result = response.choices[0].message.content.strip().strip('```json').strip().strip('```') return json.loads(result)