isayahc commited on
Commit
5dcd9c5
1 Parent(s): c0245c9

experimenting with outputting item components

Browse files
Files changed (1) hide show
  1. langhchain_generate_components.py +141 -0
langhchain_generate_components.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # import dotenv
34
+ import os
35
+ from dotenv import load_dotenv
36
+
37
+ os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
38
+
39
+
40
+ # The scheme for creating experiments
41
+ experiment_schema = [
42
+ ResponseSchema(name="Material", description="list of materials need to perfrom the experiments please be specific", type="list"),
43
+ ]
44
+
45
+
46
+ maker_schema = [
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", type="list"),
48
+ ]
49
+
50
+ experiment_output_parser = StructuredOutputParser.from_response_schemas(experiment_schema)
51
+ maker_output_parser = StructuredOutputParser.from_response_schemas(maker_schema)
52
+
53
+ memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
54
+
55
+ format_instructions = experiment_output_parser.get_format_instructions()
56
+
57
+
58
+ experiment_prompt = PromptTemplate(
59
+ template="You must generate well detailed science experiments.\n{format_instructions}\n{question}\n{context}",
60
+ input_variables=["question"],
61
+ partial_variables={"format_instructions": format_instructions},
62
+ memory = memory
63
+ )
64
+
65
+ maker_prompt = PromptTemplate(
66
+ template="You must generate a well detailed list of items for creating a given item from scratch.\n{format_instructions}\n{question}\n{context}",
67
+ input_variables=["question"],
68
+ partial_variables={"format_instructions": format_instructions},
69
+ memory = memory
70
+ )
71
+
72
+
73
+ def join_strings(*args: str) -> str:
74
+ """
75
+ Join an arbitrary number of strings into one string.
76
+
77
+ Args:
78
+ *args: Variable number of strings to join.
79
+
80
+ Returns:
81
+ str: Joined string.
82
+ """
83
+ return ''.join(args)
84
+
85
+ def format_docs(docs):
86
+ return "\n\n".join([join_strings(d.page_content, d.metadata['Entry ID'],d.metadata['Title'], ) for d in docs])
87
+
88
+
89
+ arxiv_retriever = ArxivRetriever(load_max_docs=2)
90
+
91
+ # model = ChatOpenAI(temperature=0)
92
+ model = ChatOpenAI(temperature=0,model="gpt-4")
93
+
94
+
95
+ arxiv_retriever = ArxivRetriever(load_max_docs=2)
96
+
97
+ pub_med_retriever = PubMedRetriever()
98
+
99
+ wikipedia_retriever = WikipediaRetriever()
100
+
101
+ arxiv_chain = (
102
+ {"context": arxiv_retriever, "question": RunnablePassthrough()}
103
+ | experiment_prompt
104
+ | model
105
+ | experiment_output_parser
106
+ )
107
+
108
+ pub_med_chain = (
109
+ {"context": pub_med_retriever, "question": RunnablePassthrough()}
110
+ | experiment_prompt
111
+ | model
112
+ | experiment_output_parser
113
+ )
114
+
115
+ wikipedia_chain = (
116
+ {"context": wikipedia_retriever, "question": RunnablePassthrough()}
117
+ | experiment_prompt
118
+ | model
119
+ | experiment_output_parser
120
+ )
121
+
122
+ maker_wikipedia_chain = (
123
+ {"context": wikipedia_retriever, "question": RunnablePassthrough()}
124
+ | maker_prompt
125
+ | model
126
+ | maker_output_parser
127
+ )
128
+
129
+
130
+
131
+
132
+ if __name__ == "__main__":
133
+
134
+
135
+ # query = "how to create electronoic on a cellulose subtstrate"
136
+ query = "A Microscope"
137
+
138
+ # output = wikipedia_chain.invoke(query)
139
+ output = maker_wikipedia_chain.invoke(query)
140
+ x=0
141
+