isayahc commited on
Commit
747bb1c
1 Parent(s): 930acae

chain for making apparatus

Browse files
Files changed (1) hide show
  1. structured__apparatus_chain.py +128 -0
structured__apparatus_chain.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ #TODO: make a agent that uses HUMAMN as a tool to get:
3
+ - Purpose of science experiment
4
+ - What fields of study do they already know of
5
+
6
+ #IDEA: Platform generate more indepth experiments by generaing a data set and generate / collect scienfic data
7
+
8
+ ### Chatbot
9
+ the chatbot helps the BOUNTY_BOARD_CHAIN generate science experiments
10
+
11
+ ### EXPERIMENT and Provide feedback on experiments
12
+
13
+ ### Interrgration
14
+
15
+ - I need to intergrate this code into the app. This includes creating an id for each post, and potentially and a comment section for each "Experiment"
16
+ - I addition i need to generate a mostly pinecone retriever to geenrate scientific experiments from the "community vectore search"
17
+ - potentially have prenium users store their private data, but i may not implement this during the hackathon
18
+ """
19
+
20
+ # https://python.langchain.com/docs/modules/model_io/output_parsers/types/structured
21
+ from langchain.output_parsers import ResponseSchema, StructuredOutputParser
22
+ from langchain.prompts import PromptTemplate
23
+ from langchain_openai import ChatOpenAI
24
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
25
+ from langchain.memory import ConversationBufferMemory
26
+ from langchain_core.runnables import RunnablePassthrough
27
+ from langchain.retrievers import ArxivRetriever, pubmed
28
+ from langchain_core.output_parsers import StrOutputParser
29
+ from langchain.retrievers import ArxivRetriever
30
+ from langchain.retrievers import PubMedRetriever
31
+ from langchain.retrievers import WikipediaRetriever
32
+ from operator import itemgetter
33
+
34
+ # response_schemas = [
35
+ # ResponseSchema(name="Experiment_Name", description="the name given to the experiment"),
36
+ # ResponseSchema(name="Material", description="list of materials need to perfrom the experiments", type="list"),
37
+ # ResponseSchema(name="Sources", description="list of sources where the information was retrievered from", type="list"),
38
+ # ResponseSchema(name="Protocal", description="detailed instructions On how to make the item or perform the experiment", type="list"),
39
+ # ResponseSchema(name="Fields_of_study", description="the fields of study that his experiment uses", type="list"),
40
+ # ResponseSchema(name="Purpose_of_Experiments", description="assume what the user is trying to acchieve"),
41
+ # ResponseSchema(name="Safety_Precuation", description="What does the User need to know to avoid any potential harm"),
42
+ # ResponseSchema(name="Level_of_Difficulty", description="How difficult is it to perform this experiment ecample beginner, novice, Intermidiate, Hard "),
43
+
44
+ # ]
45
+
46
+ response_schemas = [
47
+ ResponseSchema(name="Material", description="The base components needed to create this items from scratch DIY This item must be exact and not an estimation, also make sure each output has the obejcts name in context", type="list"),
48
+ ResponseSchema(name="Fields_of_study", description="List the field of study this can be used for", type="list"),
49
+ ]
50
+
51
+ output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
52
+
53
+ memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
54
+
55
+ format_instructions = output_parser.get_format_instructions()
56
+
57
+
58
+ prompt = maker_prompt = PromptTemplate(
59
+ template="You must generate a well detailed list of items for creating a given item from scratch. \
60
+ Also describe the purpose for a text-to-3d model to use for extra context\n{format_instructions}\n{question}\n{context}",
61
+ input_variables=["question"],
62
+ partial_variables={"format_instructions": format_instructions},
63
+ memory = memory
64
+ )
65
+
66
+
67
+ def join_strings(*args: str) -> str:
68
+ """
69
+ Join an arbitrary number of strings into one string.
70
+
71
+ Args:
72
+ *args: Variable number of strings to join.
73
+
74
+ Returns:
75
+ str: Joined string.
76
+ """
77
+ return ''.join(args)
78
+
79
+ def format_docs(docs):
80
+ return "\n\n".join([join_strings(d.page_content, d.metadata['Entry ID'],d.metadata['Title'], ) for d in docs])
81
+
82
+
83
+ arxiv_retriever = ArxivRetriever(load_max_docs=2)
84
+
85
+ # model = ChatOpenAI(temperature=0)
86
+ model = ChatOpenAI(temperature=0,model="gpt-4")
87
+
88
+
89
+ retriver = arxiv_retriever = ArxivRetriever(load_max_docs=2)
90
+
91
+ pub_med_retriever = PubMedRetriever()
92
+
93
+ wikipedia_retriever = WikipediaRetriever()
94
+
95
+ arxiv_chain = (
96
+ {"context": arxiv_retriever, "question": RunnablePassthrough()}
97
+ | prompt
98
+ | model
99
+ | output_parser
100
+ )
101
+
102
+ pub_med_chain = (
103
+ {"context": pub_med_retriever, "question": RunnablePassthrough()}
104
+ | prompt
105
+ | model
106
+ | output_parser
107
+ )
108
+
109
+ wikipedia_chain = (
110
+ {"context": wikipedia_retriever, "question": RunnablePassthrough()}
111
+ | prompt
112
+ | model
113
+ | output_parser
114
+ )
115
+
116
+
117
+
118
+
119
+ if __name__ == "__main__":
120
+
121
+
122
+ # query = "how to create alectronoic on a cellulose subtstrate"
123
+ query = "MicroScope"
124
+ pub_med_data = pub_med_chain.invoke(query)
125
+ wiki_data = wikipedia_chain.invoke(query)
126
+
127
+ x=0
128
+