/
main_local_gpt_4_all_openai_ner_blog_example.py
54 lines (44 loc) · 1.48 KB
/
main_local_gpt_4_all_openai_ner_blog_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.llms import GPT4All
# FASTAPI
app = FastAPI()
app.add_middleware(
CORSMiddleware, allow_origins=['*'], allow_methods=['*'], allow_headers=['*'],
)
# LANGCHAIN
gpt4_all_model_path = "./ggml-gpt4all-j-v1.3-groovy.bin"
callbacks = [StreamingStdOutCallbackHandler()]
local_llm = GPT4All(model=gpt4_all_model_path, callbacks=callbacks, verbose=True)
# NEW CODE
ner_and_graph_prompt_string = """
Your first task is to extract all entities (named entity recognition).
Secondly, create a mermaid.js graph describing the relationships between these entities.
{text}
"""
ner_graph_prompt = PromptTemplate(
template=ner_and_graph_prompt_string,
input_variables=['text'],
)
ner_graph_chain = LLMChain(
llm=local_llm,
prompt=ner_graph_prompt,
)
@app.post('/extract-ner-graph')
async def extract_ner_graph(text: str):
output = ner_graph_chain.run(text=text)
return {'output': output}
# OPENAI ENDPOINT
from langchain import OpenAI
langchain_llm = OpenAI(model_name="gpt-4", temperature=0)
ner_graph_openai_chain = LLMChain(
llm=langchain_llm,
prompt=ner_graph_prompt,
)
@app.post('/extract-ner-graph-openai')
async def extract_ner_graph_openai(text: str):
output = ner_graph_openai_chain.run(text=text)
return {'output': output}