Alors que batu mingi ba focuser na retrieval-augmented generation (RAG) likolo ya texte non structuré, lokola mikanda ya entreprise to mikanda, nazali assez bullish na ba systèmes ya retrieval sur structuré information, surtout ba graphiques ya connaissance . Ezalaki na esengo mingi na oyo etali GraphRAG, précisément mise en œuvre ya Microsoft. Kasi, na bosaleli na yango, ba données ya entrée ezali texte non structuré na ndenge ya mikanda, oyo ebongwanaka na graphique ya boyebi na kosalelaka modèle ya langue ya munene (LLM).
Na poste oyo ya blog, toko lakisa ndenge nini kosalela retriever likolo ya graphique ya boyebi oyo ezali na ba informations structurées ya FDA Adverse Event Reporting System (FAERS) , oyo epesaka ba informations na ntina ya ba événements adverses ya kisi. Soki osilá kosala ba graphiques ya boyebi mpe bozui, likanisi na yo ya liboso ekoki kozala kosalela LLM mpo na kobimisa mituna ya base de données mpo na kozwa ba sango oyo etali yango na graphique ya boyebi mpo na koyanola na motuna moko epesami. Kasi, bokeli ya mituna ya base de données na kosalelaka ba LLM ezali kaka kokola mpe ekoki kopesa naino te solution oyo ezali na boyokani to ya makasi koleka. Bongo, nini ezali ba alternatives viable na moment oyo?
Na makanisi na ngai, solution ya malamu koleka ya lelo ezali génération ya requête dynamique. Na esika ya ko se fier mobimba na LLM pona ko produire requête complete, méthode oyo esalelaka couche logique oyo e produire déterministement requête ya base de données à partir ya ba paramètres ya entrée prédéfini. Solution oyo ekoki ko mettre en œuvre na nzela ya LLM na soutien ya fonction-calling. Litomba ya kosalela fonctionnalité ya kobenga fonction ezali na makoki ya kolimbola na LLM ndenge nini esengeli kobongisa entrée structurée na fonction. Approche oyo e assure que processus ya génération ya requête ezala contrôlé pe ezala constant tout en permettant flexibilité ya entrée ya usager.
Elilingi yango ezali komonisa ndenge oyo moto azali kososola motuna ya mosaleli mpo na kozwa makambo ya sikisiki. Koleka yango esɛngaka makambo misato ya minene:
Mosaleli moko atuni motuna na ntina na ba effets secondaires oyo emonanaka mingi ya nkisi Lyrica mpo na bato oyo bakokisi naino mbula 35 te.
LLM e décider fonction nini esengeli kobenga pe ba paramètres oyo esengeli. Na exemple oyo, eponaki fonction moko na kombo ya side_effects na ba paramètres y compris kisi Lyrica na âge maximale ya 35 ans.
Fonction identifiée na ba paramètres esalelamaka pona ko générer déterministe pe dynamiquement déclaration ya requête ya base de données (Cypher) pona kozua ba informations pertinentes.
Lisungi ya kobenga misala ezali na ntina mingi mpo na makambo ya bosaleli ya LLM ya likolo, lokola kopesa nzela na ba LLM kosalela ba retrievers ebele oyo esalemi na mokano ya mosaleli to kotonga ba flux ya ba agents ebele. Nakomi mwa masolo na kosalelaka ba LLM ya mombongo oyo ezali na lisungi ya kobenga mosala native. Kasi, tokosalela Llama-3.1 oyo euti kobima, LLM ya source ouverte ya likolo oyo ezali na lisungi ya kobenga fonction native.
Code ezali na GitHub .
Tokosalela Neo4j, oyo ezali base de données ya graphique native, mpo na kobomba ba informations ya événement adverse. Okoki kosala projet ya Sandbox ya cloud ya ofele oyo eyaka na FAERS pré-populé na kolanda lien oyo .
Instance ya base de données instantiée ezali na graphique na schéma oyo elandi.
Schéma yango etali mingi node Case, oyo ekangisaka makambo ndenge na ndenge ya rapport ya sécurité ya kisi, na kati na yango ba kisi oyo etali yango, ba réactions oyo esalemi, ba résultats, mpe ba traitements oyo epesameli. Nkisi mokomoko emonisami soki ezali ya liboso, ya mibale, oyo esalemaka mbala moko to oyo esalaka na boyokani. Ba cas ezali mpe na boyokani na ba informations oyo etali fabricant, groupe ya âge ya maladi, mpe source ya rapport. Schéma oyo epesaka nzela ya kolandela mpe kotalela boyokani kati na bankisi, ba réactions na yango, mpe ba résultats na ndenge ya structuré.
Tokobanda na kosala connexion na base de données na instantiation ya objet Neo4jGraph:
os.environ["NEO4J_URI"] = "bolt://18.206.157.187:7687" os.environ["NEO4J_USERNAME"] = "neo4j" os.environ["NEO4J_PASSWORD"] = "elevation-reservist-thousands" graph = Neo4jGraph(refresh_schema=False)
Ezali na ba options ebele ya ko héberger ba LLM ya source ouverte lokola Llama-3.1. Tokosalela catalogue ya API ya NVIDIA , oyo epesaka ba microservices ya inférence ya NVIDIA NIM mpe esungaka fonction oyo ezali kobenga ba modèles ya Llama 3.1. Tango osali compte, ozuaka 1.000 jetons, oyo ekoki koleka pona kolanda. Esengeli osala fungola ya API mpe osala kopi na yango na kaye:
os.environ["NVIDIA_API_KEY"] = "nvapi-" llm = ChatNVIDIA(model="meta/llama-3.1-70b-instruct")
Tokosalela llama-3.1–70b mpo version 8b ezali na mwa ba hiccups na ba paramètres optionnels na ba définitions ya fonction.
Eloko ya malamu na ba microservices NVIDIA NIM ezali que okoki ko héberger yango facilement localement soki ozali na sécurité to ba soucis misusu, yango wana ezali facilement swappable, pe esengeli kaka obakisa paramètre URL na configuration ya LLM:
# connect to an local NIM running at localhost:8000, # specifying a specific model llm = ChatNVIDIA( base_url="http://localhost:8000/v1", model="meta/llama-3.1-70b-instruct" )
Toko configurer outil moko na ba paramètres minei optionnels. Tokotonga déclaration ya Cypher oyo ekokani na yango na kotalaka ba paramètres wanampo na kozua ba informations pertinentes na graphique ya connaissance. Esaleli na biso ekozala na makoki ya koyeba ba effets secondaires oyo emonanaka mingi na kotalaka nkisi oyo ekɔtisami, mbula, mpe mosali ya nkisi.
@tool def get_side_effects( drug: Optional[str] = Field( description="disease mentioned in the question. Return None if no mentioned." ), min_age: Optional[int] = Field( description="Minimum age of the patient. Return None if no mentioned." ), max_age: Optional[int] = Field( description="Maximum age of the patient. Return None if no mentioned." ), manufacturer: Optional[str] = Field( description="manufacturer of the drug. Return None if no mentioned." ), ): """Useful for when you need to find common side effects.""" params = {} filters = [] side_effects_base_query = """ MATCH (c:Case)-[:HAS_REACTION]->(r:Reaction), (c)-[:IS_PRIMARY_SUSPECT]->(d:Drug) """ if drug and isinstance(drug, str): candidate_drugs = [el["candidate"] for el in get_candidates(drug, "drug")] if not candidate_drugs: return "The mentioned drug was not found" filters.append("d.name IN $drugs") params["drugs"] = candidate_drugs if min_age and isinstance(min_age, int): filters.append("c.age > $min_age ") params["min_age"] = min_age if max_age and isinstance(max_age, int): filters.append("c.age < $max_age ") params["max_age"] = max_age if manufacturer and isinstance(manufacturer, str): candidate_manufacturers = [ el["candidate"] for el in get_candidates(manufacturer, "manufacturer") ] if not candidate_manufacturers: return "The mentioned manufacturer was not found" filters.append( "EXISTS {(c)<-[:REGISTERED]-(:Manufacturer {manufacturerName: $manufacturer})}" ) params["manufacturer"] = candidate_manufacturers[0] if filters: side_effects_base_query += " WHERE " side_effects_base_query += " AND ".join(filters) side_effects_base_query += """ RETURN d.name AS drug, r.description AS side_effect, count(*) AS count ORDER BY count DESC LIMIT 10 """ print(f"Using parameters: {params}") data = graph.query(side_effects_base_query, params=params) return data
get_side_effectsfunction esalemi mpo na kozwa ba effets secondaires oyo emonanaka mingi ya ba kisi na graphique ya boyebi na kosalelaka ba critères ya boluki oyo elakisami. Ezali kondima ba paramètres optionnels mpo na kombo ya kisi, âge ya maladi, mpe fabricant ya kisi mpo na ko personnaliser boluki. Paramètre moko na moko ezali na ndimbola oyo elekisami na LLM elongo na ndimbola ya mosala, kopesa nzela na LLM kososola ndenge ya kosalela yango. Na sima fonction etonga requête dynamique Cypher sur la base ya ba entrées oyo epesami, e exécuter requête oyo contre graphique ya connaissance, pe ezongisaka ba données ya ba effets secondaires oyo ezuami.
Tomeka mosala yango:
get_side_effects("lyrica") # Using parameters: {'drugs': ['LYRICA', 'LYRICA CR']} # [{'drug': 'LYRICA', 'side_effect': 'Pain', 'count': 32}, # {'drug': 'LYRICA', 'side_effect': 'Fall', 'count': 21}, # {'drug': 'LYRICA', 'side_effect': 'Intentional product use issue', 'count': 20}, # {'drug': 'LYRICA', 'side_effect': 'Insomnia', 'count': 19}, # ...
Esaleli na biso esalaki liboso carte ya kisi ya Lyrica oyo elobelami na motuna na ba valeurs “[‘LYRICA’, ‘LYRICA CR’]” na graphique ya boyebi, sima e exécuter déclaration Cypher correspondante mpo na koluka ba effets secondaires oyo emonanaka mingi.
Eloko kaka moko etikali ya kosala ezali ko configurer agent LLM oyo akoki kosalela esaleli oyo elimbolami mpo na koyanola na mituna oyo etali ba effets secondaires ya kisi.
Elilingi yango ezali komonisa mosaleli moko azali kosala boyokani na agent Llama 3.1 mpo na kotuna mpo na ba effets secondaires ya nkisi. Agent a accéder na esaleli ya ba effets secondaires oyo ezuaka ba informations na graphique ya connaissance pona kopesa usager ba données oyo etali yango.
Tokobanda na kolimbola modèle ya prompt:
prompt = ChatPromptTemplate.from_messages( [ ( "system", "You are a helpful assistant that finds information about common side effects. " "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 " "Do only the things the user specifically requested. ", ), MessagesPlaceholder(variable_name="chat_history"), ("user", "{input}"), MessagesPlaceholder(variable_name="agent_scratchpad"), ] )
Modèle ya prompt ezali na message ya système, histoire ya chat optionnel, mpe entrée ya mosaleli. Agent_scratchpad ezali réservé pona LLM, lokola tango mosusu esengeli na ba étapes ebele pona ko répondre na question, lokola ko exécuter pe kozua ba informations na ba outils.
Bibliothèque ya LangChain ekomisaka yango semba kobakisa bisaleli na LLM na kosalelaka méthode bind_tools:
tools = [get_side_effects] llm_with_tools = llm.bind_tools(tools=tools) agent = ( { "input": lambda x: x["input"], "chat_history": lambda x: _format_chat_history(x["chat_history"]) if x.get("chat_history") else [], "agent_scratchpad": lambda x: format_to_openai_function_messages( x["intermediate_steps"] ), } | prompt | llm_with_tools | OpenAIFunctionsAgentOutputParser() ) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True).with_types( input_type=AgentInput, output_type=Output )
Agent a traité entrée na nzela ya ba transformations na ba handlers oyo e formataka histoire ya chat, e appliquer LLM na ba outils bound, pe e parser sortie. Na suka, agent esalemi na exécuteur oyo e gérer flux ya exécution, elakisaka ba types ya entrée na sortie, pe esangisi ba paramètres ya verbosité pona enregistrement détaillé na tango ya exécution.
Tomeka agent yango:
agent_executor.invoke( { "input": "What are the most common side effects when using lyrica for people below 35 years old?" } )
Ba resultat:
LLM emonisaki ete esengeli kosalela fonction get_side_effects na ba arguments oyo ebongi. Na sima fonction ebimisaka dynamiquement déclaration Cypher, ezuaka ba informations pertinentes, pe ezongisaka yango na LLM pona ko produire réponse ya suka.
Makoki ya kobenga misala ezali kobakisa ya makasi na ba modèles ya source ouverte lokola Llama 3.1, ko permettre ba interactions structurées mpe contrôlées mingi na ba sources ya ba données mpe bisaleli ya libanda. Longola kaka kotuna mikanda oyo ebongisami te, ba agents oyo esalemi na ba graphiques epesaka ba possibilités ya kosepelisa mpo na kosala interaction na ba graphiques ya boyebi mpe ba données structurées. Facilité ya ko héberger ba modèles oyo en utilisant ba plateformes lokola ba microservices NVIDIA NIM ekomisaka yango plus accessible.
Lokola ntango nyonso, code ezali na GitHub .
Mpo na koyeba makambo mingi na ntina ya likambo oyo, sangana na biso na NODES 2024 le 7 novembre, conférence na biso ya ofele ya ba développeurs virtuels na ba apps ya mayele, ba graphiques ya boyebi, mpe AI. Inscrivez-vous sikoyo!