jinysun commited on
Commit
04ed4c0
·
verified ·
1 Parent(s): 9e22a3a

Update tool/rag.py

Browse files
Files changed (1) hide show
  1. tool/rag.py +101 -101
tool/rag.py CHANGED
@@ -1,101 +1,101 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- Created on Sun Feb 2 20:31:22 2025
4
-
5
- @author: BM109X32G-10GPU-02
6
- """
7
-
8
-
9
- from langchain.tools import BaseTool
10
-
11
- from langchain.prompts.chat import (
12
- ChatPromptTemplate,
13
- HumanMessagePromptTemplate,
14
- SystemMessagePromptTemplate,
15
- )
16
-
17
- from langchain import PromptTemplate
18
- from langchain import HuggingFacePipeline
19
-
20
- from langchain.base_language import BaseLanguageModel
21
-
22
-
23
-
24
- from langchain.chains import RetrievalQA
25
-
26
- from langchain_community.document_loaders import PyPDFLoader
27
-
28
- from langchain_openai import ChatOpenAI
29
- from langchain_community.vectorstores import FAISS
30
- from torch import cuda, bfloat16
31
- device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'
32
- from langchain_openai import OpenAIEmbeddings
33
- embeddings = OpenAIEmbeddings(api_key='sk-itPrztYm9F6XZZpsBMJB9O7Vq0pYUABVVBSoThuBxEGTnDik',
34
- base_url="https://www.dmxapi.com/v1")
35
- vectorstore=FAISS.load_local(r"J:\libray\osc\tool\rag", embeddings,allow_dangerous_deserialization =True)
36
-
37
-
38
- template = """
39
-
40
- You are an expert chemist and your task is to respond to the question or
41
- solve the problem to the best of your ability.You can only respond with a single "Final Answer" format.
42
- You need to list the key points and explain them in detail and accurately
43
- Use the following pieces of context to answer the question at the end.
44
- If you don't know the answer, just say that you don't know, don't try to make up an answer.
45
- <context>
46
- {context}
47
- </context>
48
-
49
- Question: {question}
50
- Answer:
51
-
52
- """
53
-
54
-
55
- class rag(BaseTool):
56
- name: str = "RAG"
57
- description: str= (
58
- "Useful to answer questions that require technical "
59
-
60
- "Provide specialized knowledge information for solving Q&A questions"
61
- "Input query , return the response"
62
-
63
- )
64
-
65
- llm: BaseLanguageModel = None
66
- path : str = None
67
-
68
- def __init__(self, path: str = None):
69
- super().__init__( )
70
- self.llm = ChatOpenAI(model="gpt-4o-2024-11-20",api_key='sk-itPrztYm9F6XZZpsBMJB9O7Vq0pYUABVVBSoThuBxEGTnDik',
71
- base_url="https://www.dmxapi.com/v1")
72
- self.path = path
73
- # api keys
74
-
75
- def _run(self, query ) -> str:
76
-
77
- prompt = PromptTemplate(template=template, input_variables=[ "question"])
78
- qa_chain = RetrievalQA.from_chain_type(
79
- llm=self.llm,
80
- chain_type="stuff",
81
- retriever=vectorstore.as_retriever(search_kwargs={"k": 5}),
82
- return_source_documents=False,
83
-
84
- chain_type_kwargs={"prompt": prompt},
85
- )
86
- chat_history = []
87
-
88
-
89
- result = qa_chain.invoke(query)
90
- return result['result']
91
-
92
-
93
- async def _arun(self, query) -> str:
94
- """Use the tool asynchronously."""
95
- raise NotImplementedError("this tool does not support async")
96
-
97
-
98
-
99
-
100
-
101
-
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Sun Feb 2 20:31:22 2025
4
+
5
+ @author: BM109X32G-10GPU-02
6
+ """
7
+
8
+
9
+ from langchain.tools import BaseTool
10
+
11
+ from langchain.prompts.chat import (
12
+ ChatPromptTemplate,
13
+ HumanMessagePromptTemplate,
14
+ SystemMessagePromptTemplate,
15
+ )
16
+
17
+ from langchain import PromptTemplate
18
+ from langchain import HuggingFacePipeline
19
+
20
+ from langchain.base_language import BaseLanguageModel
21
+
22
+
23
+
24
+ from langchain.chains import RetrievalQA
25
+
26
+ from langchain_community.document_loaders import PyPDFLoader
27
+
28
+ from langchain_openai import ChatOpenAI
29
+ from langchain_community.vectorstores import FAISS
30
+ from torch import cuda, bfloat16
31
+ device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'
32
+ from langchain_openai import OpenAIEmbeddings
33
+ embeddings = OpenAIEmbeddings(api_key='sk-itPrztYm9F6XZZpsBMJB9O7Vq0pYUABVVBSoThuBxEGTnDik',
34
+ base_url="https://www.dmxapi.com/v1")
35
+ vectorstore=FAISS.load_local(r"tool\rag", embeddings,allow_dangerous_deserialization =True)
36
+
37
+
38
+ template = """
39
+
40
+ You are an expert chemist and your task is to respond to the question or
41
+ solve the problem to the best of your ability.You can only respond with a single "Final Answer" format.
42
+ You need to list the key points and explain them in detail and accurately
43
+ Use the following pieces of context to answer the question at the end.
44
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
45
+ <context>
46
+ {context}
47
+ </context>
48
+
49
+ Question: {question}
50
+ Answer:
51
+
52
+ """
53
+
54
+
55
+ class rag(BaseTool):
56
+ name: str = "RAG"
57
+ description: str= (
58
+ "Useful to answer questions that require technical "
59
+
60
+ "Provide specialized knowledge information for solving Q&A questions"
61
+ "Input query , return the response"
62
+
63
+ )
64
+
65
+ llm: BaseLanguageModel = None
66
+ path : str = None
67
+
68
+ def __init__(self, path: str = None):
69
+ super().__init__( )
70
+ self.llm = ChatOpenAI(model="gpt-4o-2024-11-20",api_key='sk-itPrztYm9F6XZZpsBMJB9O7Vq0pYUABVVBSoThuBxEGTnDik',
71
+ base_url="https://www.dmxapi.com/v1")
72
+ self.path = path
73
+ # api keys
74
+
75
+ def _run(self, query ) -> str:
76
+
77
+ prompt = PromptTemplate(template=template, input_variables=[ "question"])
78
+ qa_chain = RetrievalQA.from_chain_type(
79
+ llm=self.llm,
80
+ chain_type="stuff",
81
+ retriever=vectorstore.as_retriever(search_kwargs={"k": 5}),
82
+ return_source_documents=False,
83
+
84
+ chain_type_kwargs={"prompt": prompt},
85
+ )
86
+ chat_history = []
87
+
88
+
89
+ result = qa_chain.invoke(query)
90
+ return result['result']
91
+
92
+
93
+ async def _arun(self, query) -> str:
94
+ """Use the tool asynchronously."""
95
+ raise NotImplementedError("this tool does not support async")
96
+
97
+
98
+
99
+
100
+
101
+