Package: llmgraph
llmgraphCreate knowledge graphs with LLMs.llmgraph enables you to create knowledge graphs inGraphML,GEXF, and HTML formats (generated viapyvis) from a given source entity Wikipedia page. The knowledge graphs are generated by extracting world knowledge from ChatGPT or other large language models (LLMs) as supported byLiteLLM.For a background on knowledge graphs see ayoutube overview by ComputerphileFeaturesCreate knowledge graphs, given a source entity.Uses ChatGPT (or another specified LLM) to extract world knowledge.Generate knowledge graphs in HTML, GraphML, and GEXF formats.Many entity types and relationships supported bycustomised prompts.Cache support to iteratively grow a knowledge graph, efficiently.Outputstotal tokensused to understand LLM costs (even though a default run is only about 1 cent).Customisable model (default is OpenAIgpt-3.5-turbofor speed and cost).InstallationYou can install llmgraph using pip, ideally into a Pythonvirtual environment:pipinstallllmgraphExample OutputIn addition to GraphML and GEXF formats, an HTMLpyvisphysics enabled graph can be viewed:Artificial Intelligence exampleGenerate above machine-learning graph:llmgraph machine-learning "https://en.wikipedia.org/wiki/Artificial_intelligence" --levels 4View entire graph:machine-learning_artificial-intelligence_v1.0.0_level4_fully_connected.htmlllmgraph UsageExample UsageThe example above was generated with the following command, which requires anentity_typeand a quotedentity_wikipediasouce url:llmgraphmachine-learning"https://en.wikipedia.org/wiki/Artificial_intelligence"--levels3This example creates a 3 level graph, based on the given start nodeArtificial Intelligence.By default OpenAI is used and you will need to set an environment variable 'OPENAI_API_KEY' prior to running. See theOpenAI docsfor more info. Thetotal tokens usedis output as the run progresses. For reference this 3 level example used a total of 7,650 gpt-3.5-turbo tokens, which is approx 1.5 cents as of Oct 2023.You can also specify a different LLM provider, including running with a localollamamodel. You should be able to specify anything supported byLiteLLMas described here:https://docs.litellm.ai/docs/providers. Note that the prompts to extract related entities were tested with OpenAI and may not work as well with other models.Localollama/llama2model example:llmgraphmachine-learning"https://en.wikipedia.org/wiki/Artificial_intelligence"--levels3--llm-modelollama/llama2--llm-base-urlhttp://localhost:<your_port>Theentity_typesets the LLM prompt used to find related entities to include in the graph. The full list can be seen inprompts.yamland include the following entity types:automobilebookcomputer-gameconcepts-generalcreative-generaldocumentaryfoodmachine-learningmoviemusicpeople-historicalpodcastsoftware-engineeringtvRequired Argumentsentity_type(TEXT): Entity type (e.g. movie)entity_wikipedia(TEXT): Full Wikipedia link to the root entityOptional Arguments--entity-root(TEXT): Optional root entity name override if different from the Wikipedia page title [default: None]--levels(INTEGER): Number of levels deep to construct from the central root entity [default: 2]--max-sum-total-tokens(INTEGER): Maximum sum of tokens for graph generation [default: 200000]--output-folder(TEXT): Folder location to write outputs [default: ./_output/]--llm-model(TEXT): The model name [default: gpt-3.5-turbo]--llm-temp(FLOAT): LLM temperature value [default: 0.0]--llm-base-url(TEXT): LLM will use custom base URL instead of the automatic one [default: None]--version: Display llmgraph version and exit.--help: Show this message and exit.More Examples of HTML OutputHere are some more examples of the HTML graph output for different entity types and root entities (with commands to generate and links to view full interactive graphs).Install llmgraph to create your own knowledge graphs! Feel free to share interesting results in theissue sectionabove with a documentation label :)Knowledge graph concept exampleCommand to generate above concepts-general graph:llmgraph concepts-general "https://en.wikipedia.org/wiki/Knowledge_graph" --levels 4View entire graph:concepts-general_knowledge-graph_v1.0.0_level4_fully_connected.htmlInception movie exampleCommand to generate above movie graph:llmgraph movie "https://en.wikipedia.org/wiki/Inception" --levels 4View entire graph:movie_inception_v1.0.0_level4_fully_connected.htmlOpenAI company exampleCommand to generate above company graph:llmgraph company "https://en.wikipedia.org/wiki/OpenAI" --levels 4View entire graph:company_openai_v1.0.0_level4_fully_connected.htmlJohn von Neumann people exampleCommand to generate above people-historical graph:llmgraph people-historical "https://en.wikipedia.org/wiki/John_von_Neumann" --levels 4View entire graph:people-historical_john-von-neumann_v1.0.0_level4_fully_connected.htmlExample of Prompt Used to Generate GraphHere is an example of the prompt template, with place holders, used to generate related entities from a given source entity. This is applied recursively to create a knowledge graph, merging duplicated nodes as required.You are knowledgeable about {knowledgeable_about}.
List, in json array format, the top {top_n} {entities} most like '{{entity_root}}'
with Wikipedia link, reasons for similarity, similarity on scale of 0 to 1.
Format your response in json array format as an array with column names: 'name', 'wikipedia_link', 'reason_for_similarity', and 'similarity'.
Example response: {{{{"name": "Example {entity}","wikipedia_link": "https://en.wikipedia.org/wiki/Example_{entity_underscored}","reason_for_similarity": "Reason for similarity","similarity": 0.5}}}}It works well on the primary tested LLM, being OpenAI gpt-3.5-turbo. Results are ok, but not as good using Llama2. The prompt source of truth and additional details can be see inprompts.yaml.Each entity type has custom placeholders, for exampleconcepts-generalanddocumentary:concepts-general:
    system: You are a highly knowledgeable ontologist and creator of knowledge graphs.
    knowledgeable_about: many concepts and ontologies.
    entities: concepts
    entity: concept name
    top_n: 5

documentary:
    system: You are knowledgeable about documentaries of all types, and genres.
    knowledgeable_about: documentaries of all types, and genres
    entities: Documentaries
    entity: Documentary
    top_n: 5Cached LLM API callsEach call to the LLM API (and Wikipedia) is cached locally in a.joblib_cachefolder. This allows an interrupted run to be resumed without duplicating identical calls. It also allows a re-run with a higher--leveloption to re-use results from the lower level run (assuming the same entity type and source).Future ImprovementsContrast graph output from different LLM models (e.g.Llama2vsMistralvsChatGPT-4)Investigate the hypothosis that this approach provides insight into how an LLM views the world.Include more examples in this documentation and make examples available for easy browsing.Instructions for running locally and adding a customentity_typeprompt.Better pyviz html output, in particular including reasons for entity relationship in UI and arguments for pixel size etc.Parallelise API calls and result processing.Remove dependency on Wikipedia entities as a source.Contrast results from llmgraphg with other non-LLM graph construction e.g. using wikipedia page links, ordirect article embeddings.ContributingContributions to llmgraph are welcome. Please follow these steps:Fork the repository.Create a new branch for your feature or bug fix.Make your changes and commit them.Create a pull request with a description of your changes.Thanks 🙏Thanks to @breitburg for implementing the LiteLLM updates.Referenceshttps://arxiv.org/abs/2211.10511- Knowledge Graph Generation From Texthttps://arxiv.org/abs/2310.04562- Towards Foundation Models for Knowledge Graph Reasoninghttps://arxiv.org/abs/2206.14268- BertNet: Harvesting Knowledge Graphs with Arbitrary Relations from Pretrained Language Modelshttps://github.com/aws/graph-notebook- Graph Notebook: easily query and visualize graphshttps://github.com/KiddoZhu/NBFNet-PyG- PyG re-implementation of Neural Bellman-Ford Networkshttps://caminao.blog/knowledge-management-booklet/a-hitchhikers-guide-to-knowledge-galaxies/- A Hitchhiker’s Guide to Knowledge Galaxies