在官方例子中给出了通过chain = NebulaGraphQAChain.from_llm(ChatOpenAI(temperature=0), graph=graph, verbose=True)来检索NebulaGraph图数据库。本文介绍了通过GPT2替换ChatOpenAI的思路和实现,暂时不考虑效果。之所以没用ChatGLM2是因为加载模型太慢,调试不方便,不过将GPT2替换为ChatGLM2也很方便。
一.通过ChatOpenAI来检索NebulaGraph1.NebulaGraph_OpenAI.py代码实现 如果没有ChatGPT的key和proxy是没法运行的,如下所示:
2.NebulaGraphQAChain默认prompt 基本思路是介绍、举例、图Schema和限制,如下所示:
二.通过GPT2来检索NebulaGraph1.NebulaGraph_GPT2.py代码实现 使用自定义的GPT2()替换ChatOpenAI(temperature=0)即可,如下所示:
2.GPT2.py代码实现 主要是继承LLM类,并且实现def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:函数,如下所示:
3.GPT2_Flask.py代码实现 主要是通过Flask将GPT2进行API封装,如下所示:
因为通用LLM通过prompt将text转换为nGQL并不专业,觉得以后的发展思路应该还是专用LLM作为agent来做这个事情。
参考文献:[1]https://huggingface.co/gpt2[2]使用LLMs模块接入自定义大模型:https://blog.youkuaiyun.com/zhaomengsen/article/details/130585397[3]https://github.com/ai408/Langchain-Chatchat/blob/master/examples/NebulaGraph_GPT2.py[4]https://github.com/ai408/Langchain-Chatchat/blob/master/examples/GPT2.py[5]https://github.com/ai408/Langchain-Chatchat/blob/master/examples/GPT2_Flask.py