Skip to content

Conversation

@Bearnardd
Copy link
Contributor

fixes #7289
A simple fix of the buggy output of graph_qa. If we have several entities with triplets then the last entry of triplets for a given entity merges with the first entry of the triplets of the next entity.

Example

from langchain.llms import OpenAI
from langchain.indexes import GraphIndexCreator
from langchain.chains import GraphQAChain
from langchain.prompts import PromptTemplate
import os

text = "Apple announced the Vision Pro in 2023."

openai_api_key = os.getenv("OPENAI_API_KEY")

index_creator = GraphIndexCreator(llm=OpenAI(openai_api_key=openai_api_key, temperature=0))
graph = index_creator.from_text(text)
chain = GraphQAChain.from_llm(
    OpenAI(temperature=0, openai_api_key=openai_api_key),
    graph=graph, 
    verbose=True
)

chain.run("When did Apple announce the Vision Pro?")

# output pre-fix
Entities Extracted:
Apple, Vision Pro
Full Context:
Apple announced Vision ProVision Pro was announced in 2023

> Finished chain.

# output post-fix
Entities Extracted:
Apple, Vision Pro
Full Context:
Apple announced Vision Pro
Vision Pro was announced in 2023


> Finished chain.

@baskaryan

@vercel
Copy link

vercel bot commented Jul 7, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Jul 7, 2023 9:11pm

@dosubot dosubot bot added the bug Related to a bug, vulnerability, unexpected error with an existing feature label Jul 7, 2023
@Bearnardd
Copy link
Contributor Author

If the output with additional "\n" at the end is somehow problematic we can a add check if the current entity is the last one

for entity in entities:
triplets = self.graph.get_entity_knowledge(entity)
context += "\n".join(triplets)
context += "\n".join(triplets) + "\n"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just so there's not a hanging \n at end of context, maybe:

all_triplets = []
for entity in entities:
  all_triplets.extend(self.graph.get_entity_knowledge(entity))
context = "\n".join(all_triplets)

would be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@baskaryan Yeah, way cleaner!

@Bearnardd Bearnardd requested a review from baskaryan July 7, 2023 21:01
@baskaryan
Copy link
Collaborator

baskaryan commented Jul 7, 2023

thanks @Bearnardd!

@baskaryan baskaryan merged commit 184ede4 into langchain-ai:master Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Related to a bug, vulnerability, unexpected error with an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Input in GraphQAChain Prompt is Malformed When Only 1 Triplet Is Found

2 participants