-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
51 lines (41 loc) · 1.97 KB
/
parse.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
import os
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_groq.chat_models import ChatGroq
from langchain_community.llms import HuggingFaceHub
from dotenv import load_dotenv
from langchain.schema import AIMessage
load_dotenv()
os.environ['HUGGINGFACEHUB_API_TOKEN'] = os.getenv('HUGGINGFACEHUB_API_TOKEN')
groq_api_key = os.getenv('groq_api_key')
llm1 = ChatGroq(groq_api_key=groq_api_key, model_name="llama-3.1-8b-instant")
# llm2 = HuggingFaceHub(
# repo_id="meta-llama/Meta-Llama-3-8B",
# task="text-generation",
# model_kwargs={
# "max_new_tokens": 512,
# "top_k": 30,
# "temperature": 0.1,
# "repetition_penalty": 1.03,
# },
# )
template = (
"You are tasked with extracting specific information from the following text content: {dom_content}. "
"Please follow these instructions carefully: \n\n"
"1. **Extract Information:** Only extract the information that directly matches the provided description: {parse_description}. "
"2. **No Extra Content:** Do not include any additional text, comments, or explanations in your response. "
"3. **Empty Response:** If no information matches the description, return an empty string ('')."
"4. **Direct Data Only:** Your output should contain only the data that is explicitly requested, with no other text."
)
def handle_userinput(dom_chunks, parse_description):
prompt = ChatPromptTemplate.from_template(template)
chain = prompt | llm1
parsed_results = []
print("Prompting results")
for i, chunk in enumerate(dom_chunks, start=1):
response = chain.invoke(
{"dom_content": chunk, "parse_description": parse_description}
)
print(f"Parsed batch: {i} of {len(dom_chunks)}")
parsed_results.append(response)
return "\n".join([message.content if isinstance(message, AIMessage) else str(message) for message in parsed_results])