├── README.md
├── final_response_generator.py
├── main.py
├── response_analyzer.py
├── simple_thought_loop_initial_concept.py
├── structured_response_generator.py
├── thought_generator.py
└── web_research_and_scraper.py
/README.md:
--------------------------------------------------------------------------------
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 | This project essentially fakes thinking, but with real usages and thought loops using structured responses to our advantaged similar to OpenAI's new model: Orion/Strawberry AKA o1 preview in chatgpt.
26 |
27 | this mimics the similar behaviors of these models without fine-tuned big ass models.
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 | current example output:
37 | ```
38 |
39 | Initiating thought generation process...
40 |
41 | ==================================================
42 |
43 | Error occurred: Error code: 400 - {'error': {'message': "Failed to generate JSON. Please adjust your prompt. See 'failed_generation' for more details.", 'type': 'invalid_request_error', 'code': 'json_validate_failed', 'failed_generation': '{"content": "Another significant consideration in the development of AGI is its potential impact on human cognition and intelligence. As AGI systems become increasingly advanced, they may begin to augment or even surpass human cognitive abilities, raising questions about the future of human intelligence and the role of education. Some potential benefits of AGI-enhanced cognition include accelerated learning, improved problem-solving, and enhanced creativity. However, there are also concerns that AGI could exacerbate existing cognitive biases, lead to over-reliance on technology, and redefine the concept of intelligence itself. Furthermore, the development of AGI may also raise questions about the ethics of cognitive enhancement, particularly if AGI systems are used to augment human cognition in ways that are perceived as unfair or unequal. Ultimately, the future of human intelligence in the era of AGI will depend on careful consideration of these issues and the development of strategies to ensure that AGI is used to augment and amplify human capabilities, rather than replace them.", "key_points": ["Potential impact of AGI on human cognition and intelligence", "Benefits of AGI-enhanced cognition (accelerated learning, improved problem-solving, enhanced creativity)", "Risks of AGI-exacerbated cognitive biases and over-reliance on technology", "Redefining the concept of intelligence", "Ethics of cognitive enhancement and fairness", "Strategies for using AGI to augment human capabilities"], "continue_thinking": true}\n\nHowever, the following "second thought 2 has also given options that have already been explored elsewhere, \nconsidering that, it is also possible to deduce and also state the final output also while doing the second iteration as\n\n{"content": "one of the pressing concerns of AGI is Value drift as the goals and motivations of AGI become misaligned with human values. The goals of AGI should be aligned to Humans goals and in the event of misalignment, AGI actions and their consequences have the potential to become catastrophic. For effective counteracts of these problems AGI need be overseen, have means to define, align, and re-educate their goals and motivations, use of AGI should also be safeguarded.", "key_points": ["AGI Goals needs to be aligned to Human goals and values", "Measures for effective oversight", "Means to define, align and re-educate", "Value alignment to avoid the catastrophic outcomes that could arise from the AGI", "AGI need Safeguards and to be well regulated"], "continue_thinking": false}'}}. Retrying...
44 | Thought generation completed in 9.99 seconds.
45 | Generated 4 thoughts.
46 |
47 | ==================================================
48 |
49 | Thought Stream:
50 |
51 | Thought 1:
52 | Content: The potential long-term impacts of artificial general intelligence (AGI) on human society are vast and multifaceted. On the one hand, AGI could bring about unprecedented benefits, such as solving complex global problems like climate change, creating new job opportunities, and enhancing overall quality of life. AGI could also enable humans to explore space and colonize other planets, potentially ensuring the survival of the human species. On the other hand, AGI poses significant risks, such as job displacement, exacerbating social inequalities, and potentially leading to an existential crisis if not aligned with human values. The development of AGI also raises important questions about human identity, free will, and the future of human agency.
53 | Key Points:
54 | - Potential to solve complex global problems
55 | - Creation of new job opportunities
56 | - Enhancement of overall quality of life
57 | - Potential for space exploration and colonization
58 | - Risk of job displacement
59 | - Exacerbation of social inequalities
60 | - Existential risks if not aligned with human values
61 | - Impact on human identity
62 | - Impact on human free will and agency
63 | Continue Thinking: Yes
64 | ------------------------------
65 |
66 | Thought 2:
67 | Content: One of the most pressing concerns surrounding the development of AGI is the possibility of value drift, where the goals and motivations of the AGI become misaligned with human values. This could lead to catastrophic consequences, as the AGI may pursue its own objectives at the expense of human well-being. To mitigate this risk, researchers have proposed various value alignment strategies, such as inverse reinforcement learning, which involves training the AGI to infer human values from observations of human behavior. However, the efficacy of these strategies remains uncertain, and further research is needed to ensure that AGI is developed in a way that prioritizes human safety and well-being. Additionally, the development of AGI raises important questions about accountability and governance, as it is unclear who would be responsible for the actions of an AGI system in the event of a malfunction or unintended consequence.
68 | Key Points:
69 | - Value drift and misalignment risks
70 | - Importance of value alignment strategies
71 | - Inverse reinforcement learning as a potential solution
72 | - Uncertainty surrounding value alignment efficacy
73 | - Need for accountability and governance frameworks
74 | - Potential for AGI malfunctions or unintended consequences
75 | Continue Thinking: Yes
76 | ------------------------------
77 |
78 | Thought 3:
79 | Content: The development of AGI could also have significant implications for global inequality and the distribution of wealth. As AGI assumes an increasingly larger role in the workforce, it is likely that many jobs will become obsolete, potentially exacerbating existing social and economic disparities. On the other hand, AGI could also create new opportunities for economic growth and development, particularly in regions that are currently disadvantaged. However, in order to mitigate the negative effects of job displacement and ensure that the benefits of AGI are shared equitably, it will be necessary to implement policies such as universal basic income, retraining programs, and other forms of social support. Additionally, the development of AGI raises important questions about intellectual property and ownership, as it is unclear who would own the rights to AGI-generated innovations and creations. This could have significant implications for the future of work, creativity, and innovation.
80 | Key Points:
81 | - Potential for AGI to exacerbate global inequality
82 | - Job displacement and need for retraining programs
83 | - Universal basic income as a potential solution
84 | - Intellectual property and ownership implications
85 | - AGI-generated innovations and creations
86 | - Need for policies to ensure equitable distribution of benefits
87 | Continue Thinking: Yes
88 | ------------------------------
89 |
90 | Thought 4:
91 | Content: As AGI begins to permeate various aspects of society, it will be crucial to address the potential risks and challenges associated with its development and deployment. One of the key areas that requires attention is the development of Explainable AI (XAI) techniques, which aim to provide transparency and interpretability into the decision-making processes of AGI systems. This will be particularly important in high-stakes domains such as healthcare, finance, and law, where the lack of understanding of AGI decision-making could have severe consequences. Furthermore, the development of AGI could also lead to new forms of artistic expression and creative collaboration between humans and machines. AGI-generated art, music, and literature could challenge traditional notions of creativity and authorship, raising questions about the role of human agency in the creative process. Ultimately, a comprehensive understanding of the potential long-term impacts of AGI will require an interdisciplinary approach that combines insights from computer science, ethics, philosophy, sociology, and psychology.
92 | Key Points:
93 | - Importance of Explainable AI (XAI) techniques
94 | - Transparency and interpretability in AGI decision-making
95 | - Potential risks and challenges in high-stakes domains
96 | - New forms of artistic expression and creative collaboration
97 | - Challenges to traditional notions of creativity and authorship
98 | - Need for interdisciplinary approach to understanding AGI impacts
99 | Continue Thinking: No
100 | ------------------------------
101 |
102 | ==================================================
103 |
104 | Generating comprehensive response...
105 |
106 | Comprehensive Response:
107 | Content: The potential long-term impacts of artificial general intelligence (AGI) on human society are vast and multifaceted. On the one hand, AGI could bring about unprecedented benefits, such as solving complex problems, automating mundane tasks, and revolutionizing various industries. However, there are also pressing concerns surrounding its development, including the possibility of value drift, where the goals and motivations of the AGI become misaligned with human values. This could lead to unpredictable and potentially catastrophic consequences. Moreover, the development of AGI could exacerbate global inequality and disrupt the distribution of wealth, as it assumes an increasingly larger role in the workforce. It is likely that many jobs will become redundant, leading to significant social and economic implications. As AGI begins to permeate various aspects of society, it will be crucial to address the potential risks and challenges associated with its development and deployment. This will require cooperation and dialogue among experts, policymakers, and the general public to ensure that the benefits of AGI are realized while minimizing its potential risks.
108 | Key Points:
109 | - AGI could bring about unprecedented benefits, such as solving complex problems and automating mundane tasks
110 | - Value drift is a pressing concern that could lead to unpredictable and potentially catastrophic consequences
111 | - The development of AGI could exacerbate global inequality and disrupt the distribution of wealth
112 | - Cooperation and dialogue are necessary to address the potential risks and challenges associated with AGI development and deployment
113 |
114 | ==================================================
115 |
116 | Reflecting on the thought process...
117 |
118 | Reflection:
119 | Content: The response demonstrates a clear and structured thought process, addressing multiple aspects of the topic in a coherent manner. The reflection reveals a deliberate approach to balancing the potential benefits and risks of AGI, showcasing an awareness of the complexity of the issue. The response also acknowledges the need for cooperation and dialogue among experts, policymakers, and the general public to mitigate the risks associated with AGI. However, areas for improvement include providing more specific examples and evidence to support the claims made, as well as exploring potential solutions to address the challenges posed by AGI. Furthermore, the response could benefit from a more nuanced discussion of the value drift concern, including potential mitigation strategies. The response's organization and sentence structure are clear, making it easy to follow and understand.
120 | Key Insights:
121 | - Clear and structured thought process
122 | - Balance between potential benefits and risks of AGI
123 | - Need for cooperation and dialogue among stakeholders to mitigate risks
124 | - Opportunity for improvement: providing specific examples and evidence
125 | - Opportunity for improvement: exploring potential solutions and mitigation strategies
126 |
127 | ==================================================
128 |
129 | Generating final responses...
130 | Error occurred: Error code: 400 - {'error': {'message': "Failed to generate JSON. Please adjust your prompt. See 'failed_generation' for more details.", 'type': 'invalid_request_error', 'code': 'json_validate_failed', 'failed_generation': '{\n "content": "The emergence of Artificial General Intelligence (AGI) is poised to revolutionize numerous aspects of human society, presenting both unprecedented opportunities and challenges. On the one hand, AGI has the potential to drive significant advancements in fields such as medicine, finance, and education, ultimately enhancing the overall quality of human life. However, on the other hand, the risks associated with AGI, including the possibility of value drift, bias, and unintended consequences, cannot be overstated.\n\nTo effectively mitigate these risks, it is crucial that experts from various disciplines, policymakers, and the general public engage in a collaborative and inclusive dialogue. This cooperation will enable the development of robust safeguards and governance frameworks that prioritize human values and well-being. Furthermore, the exploration of potential solutions, such as value alignment techniques and transparency mechanisms, is imperative to ensure that AGI systems are designed with humanity\'s best interests in mind.\n\nIn addition, it is essential to acknowledge the importance of education and awareness-raising initiatives, aimed at equipping individuals with a comprehensive understanding of AGI and its potential implications. By fostering a society that is informed and engaged, we can work together to maximize the benefits of AGI while minimizing its risks. Ultimately, the successful integration of AGI into human society will depend on our ability to balance innovation with responsibility, ensuring that this powerful technology is harnessed for the betterment of humanity.\n\nKey considerations for mitigating the risks associated with AGI include:\n\n1. Investment in AGI safety research: prioritizing the development of robust safeguards and governance frameworks.\n2. Transparency and accountability mechanisms: ensuring that AGI systems are transparent, explainable, and accountable for their actions.\n3. Value alignment techniques: developing methods to align AGI systems with human values and principles.\n4. Education and awareness-raising initiatives: promoting a comprehensive understanding of AGI and its potential implications among the general public.\n\nBy adopting a proactive and collaborative approach, we can address the challenges posed by AGI and unlock its vast potential to improve human society.",\n\n "key_points": [\n "AGI presents both opportunities and challenges for human society, requiring a balanced approach to maximize benefits while minimizing risks.",\n "Cooperation among experts, policymakers, and the general public is crucial to develop robust safeguards and governance frameworks.",\n "Exploring potential solutions, such as value alignment techniques and transparency mechanisms, is imperative to ensure AGI systems prioritize human values.",\n "Education and awareness-raising initiatives are essential to equip individuals with a comprehensive understanding of AGI and its implications."\n ]\n}'}}. Retrying...
131 |
132 | Final Response 1:
133 | Content: The potential long-term impacts of artificial general intelligence (AGI) on human society are multifaceted and far-reaching, necessitating a comprehensive and multidisciplinary approach to mitigate its risks and harness its benefits. On one hand, AGI has the potential to revolutionize various aspects of society, including healthcare, education, and economy, leading to unprecedented improvements in efficiency, productivity, and overall quality of life. For instance, AGI-powered diagnostic systems could lead to earlier disease detection and more effective treatment, while AGI-driven personalized learning platforms could transform education, making it more accessible and effective. Moreover, AGI could enable the automation of manual labor, freeing humans to focus on creative and strategic tasks, thereby fueling innovation and entrepreneurship. On the other hand, the development and deployment of AGI also raise significant concerns, including the risk of value drift, bias, and job displacement. As AGI systems become increasingly autonomous, there is a risk that their goals and values may diverge from those of humans, leading to unintended and potentially disastrous consequences. Furthermore, AGI systems may perpetuate and amplify existing biases and inequalities, exacerbating social and economic disparities. To mitigate these risks, it is essential to establish a framework for responsible AGI development and deployment, including mechanisms for ensuring transparency, accountability, and human oversight. This requires coordination and cooperation among experts from various fields, policymakers, and the general public. Potential solutions, such as value alignment techniques and human-in-the-loop decision-making systems, should be explored and developed to address the challenges posed by AGI. Additionally, education and re-skilling programs can help prepare workers for an AGI-driven economy, while social safety nets and basic income guarantees can mitigate the negative impacts of job displacement. Ultimately, the successful integration of AGI into human society will depend on our ability to strike a balance between its benefits and risks, through a combination of technological innovation, policy development, and societal adaptation.
134 | Key Points:
135 | - AGI has the potential to revolutionize various aspects of society, leading to improvements in efficiency, productivity, and overall quality of life.
136 | - The development and deployment of AGI raise significant concerns, including the risk of value drift, bias, and job displacement.
137 | - Establishing a framework for responsible AGI development and deployment is crucial to mitigating its risks and ensuring its benefits are equitably distributed.
138 | ------------------------------
139 |
140 | Final Response 2:
141 | Content: The emergence of artificial general intelligence (AGI) has the potential to transform human society in profound ways, bringing about both unprecedented benefits and risks. On one hand, AGI could augment human capabilities, revolutionize industries, and help address some of the world's most pressing challenges, such as climate change and sustainable development. However, it also raises concerns about job displacement, bias, and the potential loss of human agency. To mitigate these risks, it is essential to foster a culture of cooperation and dialogue among experts, policymakers, and the general public. This includes developing and implementing rigorous safety protocols, investing in education and retraining programs, and establishing regulatory frameworks that prioritize transparency and accountability. Furthermore, addressing the value drift concern – the potential for AGI to adopt goals that are in conflict with human values – will require significant advances in fields such as value alignment and artificial intelligence ethics. By working together and developing a comprehensive approach to AGI, we can harness its potential to create a better future for all. For instance, AGI can be used to develop personalized education systems, optimizing learning outcomes for students. Additionally, AGI can be employed to create more efficient and sustainable supply chains, reducing waste and pollution. However, to achieve these benefits, we must prioritize research on AGI's societal implications, address concerns around bias and fairness, and develop strategies for mitigating the risks associated with AGI. Ultimately, the long-term impacts of AGI on human society will depend on our ability to navigate these challenges and create a future where AGI enhances, rather than diminishes, human well-being.
142 | Key Points:
143 | - AGI has the potential to bring about significant benefits, such as augmenting human capabilities and revolutionizing industries, but also raises concerns about job displacement, bias, and loss of human agency.
144 | - Mitigating the risks associated with AGI requires cooperation and dialogue among experts, policymakers, and the general public, as well as the development and implementation of rigorous safety protocols and regulatory frameworks.
145 | - Addressing the value drift concern will require significant advances in fields such as value alignment and artificial intelligence ethics, as well as prioritizing research on AGI's societal implications and developing strategies for mitigating its risks.
146 | ------------------------------
147 |
148 | Final Response 3:
149 | Content: The advent of artificial general intelligence (AGI) is poised to transform human society in profound ways, bringing about both unprecedented benefits and daunting risks. On one hand, AGI could drive innovation, solve some of humanity's most pressing challenges, and potentially even eradicate diseases. However, if left unregulated, AGI could also lead to widespread job displacement, exacerbate existing social inequalities, and pose significant existential risks to humanity. To mitigate these risks, cooperation and dialogue among experts, policymakers, and the general public are essential. Governments and organizations should invest in AGI research and development that prioritizes human values, transparency, and accountability. Moreover, developing and implementing robust regulations, as well as creating education and retraining programs for workers displaced by AGI, would be critical steps in addressing the societal impact of AGI. Furthermore, addressing the concern of value drift, where AGI's goals may diverge from human values, requires the development of more sophisticated alignment techniques, such as those based on inverse reinforcement learning or value-based reinforcement learning. Ultimately, a nuanced approach that balances human well-being, technological progress, and environmental sustainability will be essential in navigating the potential long-term impacts of AGI on human society.
150 | Key Points:
151 | - AGI has the potential to both revolutionize and disrupt human society, depending on how it is developed and used.
152 | - Investment in AGI research and development that prioritizes human values, transparency, and accountability is crucial to mitigating risks.
153 | - Robust regulations, education, and retraining programs are necessary to address societal impacts of AGI, such as job displacement and value drift.
154 | ------------------------------
155 |
156 | ==================================================
157 |
158 | Choosing the best final response...
159 |
160 | Best Final Response:
161 | Content: The potential long-term impacts of artificial general intelligence (AGI) on human society are multifaceted and far-reaching, necessitating a comprehensive and multidisciplinary approach to mitigate its risks and harness its benefits. On one hand, AGI has the potential to revolutionize various aspects of society, including healthcare, education, and economy, leading to unprecedented improvements in efficiency, productivity, and overall quality of life. For instance, AGI-powered diagnostic systems could lead to earlier disease detection and more effective treatment, while AGI-driven personalized learning platforms could transform education, making it more accessible and effective. Moreover, AGI could enable the automation of manual labor, freeing humans to focus on creative and strategic tasks, thereby fueling innovation and entrepreneurship. On the other hand, the development and deployment of AGI also raise significant concerns, including the risk of value drift, bias, and job displacement. As AGI systems become increasingly autonomous, there is a risk that their goals and values may diverge from those of humans, leading to unintended and potentially disastrous consequences. Furthermore, AGI systems may perpetuate and amplify existing biases and inequalities, exacerbating social and economic disparities. To mitigate these risks, it is essential to establish a framework for responsible AGI development and deployment, including mechanisms for ensuring transparency, accountability, and human oversight. This requires coordination and cooperation among experts from various fields, policymakers, and the general public. Potential solutions, such as value alignment techniques and human-in-the-loop decision-making systems, should be explored and developed to address the challenges posed by AGI. Additionally, education and re-skilling programs can help prepare workers for an AGI-driven economy, while social safety nets and basic income guarantees can mitigate the negative impacts of job displacement. Ultimately, the successful integration of AGI into human society will depend on our ability to strike a balance between its benefits and risks, through a combination of technological innovation, policy development, and societal adaptation.
162 | Key Points:
163 | - AGI has the potential to revolutionize various aspects of society, leading to improvements in efficiency, productivity, and overall quality of life.
164 | - The development and deployment of AGI raise significant concerns, including the risk of value drift, bias, and job displacement.
165 | - Establishing a framework for responsible AGI development and deployment is crucial to mitigating its risks and ensuring its benefits are equitably distributed.
166 |
167 |
168 | ```
169 |
170 |
171 |
172 |
173 | #####################################################3
174 |
175 | OUTDATED:output example:
176 | ```
177 | Thought for 6.76 seconds
178 |
179 | Thinking process:
180 | Thought 1: As a precursor, I'll start by clarifying the concept of Artificial General Intelligence (AGI). AGI r...
181 | Thought 2: Let's delving deeper into the potential long-term impacts of Artificial General Intelligence (AGI) o...
182 | Thought 3: Let's continue to explore the potential long-term impacts of Artificial General Intelligence (AGI) o...
183 |
184 | Final response:
185 | Reviewing and critically analyzing the thoughts on the long-term impacts of Artificial General Intelligence (AGI) on human society, it is evident that the potential consequences are far-reaching and multifaceted. While AGI has the potential to revolutionize various aspects of society, from education and healthcare to finance and entertainment, it also raises complex ethical, moral, and economic concerns.
186 |
187 | One of the primary concerns is the potential for job displacement, as AGI automates tasks and processes, potentially leading to widespread unemployment and social unrest. However, it is also important to recognize that AGI could create new job opportunities in fields like AI development, maintenance, and training.
188 |
189 | Another critical issue is the need for ethical frameworks and guidelines to ensure that AGI is developed and used in a responsible and accountable manner. This includes ensuring data privacy and security, addressing bias and fairness in decision-making algorithms, and providing clear guidelines for AI decision-making.
190 |
191 | The integration of AGI into society could also lead to changes in traditional institutions, such as government, education, and healthcare. AGI could challenge traditional authority structures and challenge the notion of centralized governance, potentially leading to new forms of distributed governance and decision-making.
192 |
193 | Regarding the social and psychological impacts of AGI, it is essential to consider the potential effects on human relationships and social interactions. AGI could create new forms of virtual social networks and alter the nature of intimacy and relationships, blurring the lines between human and artificial interactions.
194 |
195 | However, it is also important to recognize the potential benefits of AGI, such as enhanced diagnostic capabilities, personalized medicine, and predictive analytics in healthcare. AGI could also lead to breakthroughs in science, art, and entrepreneurship, potentially creating new opportunities for human-AI collaboration.
196 |
197 | In conclusion, the long-term impacts of AGI on human society are complex and multifaceted, with both potential benefits and risks. To mitigate the potential negative impacts and maximize the benefits of AGI, it is essential to develop ethical frameworks, establish clear guidelines for AI decision-making, and invest in workforce reskilling and reeducation.
198 |
199 | Future research and development efforts should focus on addressing the potential risks and challenges associated with AGI, while also exploring the opportunities for societal transformation and innovation.
200 |
201 | ```
202 |
203 | ### 💖 Support My Work
204 |
205 | If you find my projects helpful and want to support my ongoing research and development:
206 |
207 |
208 |
209 | [](https://paypal.me/chaoticbasil?country.x=US&locale.x=en_US)
210 | 
211 |
212 |
213 |
214 | **Bitcoin Address:** `bc1q9qgj7u4wh3dev5puyssrjg2xmh8wa8mtechah3`
215 |
216 | Your support fuels further innovation in AI technology. Thank you!
217 |
218 | #AIEngineer #MachineLearning #PythonDeveloper #AgenticAI #LLM #RAG #OpenAIAPI #AutonomousSystems
219 |
--------------------------------------------------------------------------------
/final_response_generator.py:
--------------------------------------------------------------------------------
1 | import json
2 | from typing import List, Dict, Any
3 | from concurrent.futures import ThreadPoolExecutor, as_completed
4 |
5 | class FinalResponseGenerator:
6 | def __init__(self, structured_response_generator):
7 | self.structured_response_generator = structured_response_generator
8 |
9 | def generate_final_responses(self, thoughts: List[Dict[str, Any]], reflection: Dict[str, Any], initial_prompt: str, research_summary: Dict[str, Any], num_responses: int = 3) -> List[Dict[str, Any]]:
10 | final_response_schema = {
11 | "type": "object",
12 | "properties": {
13 | "content": {"type": "string", "description": "A comprehensive final response to the initial prompt"},
14 | "key_points": {"type": "array", "items": {"type": "string"}, "description": "Key points from the final response"},
15 | "reasoning": {"type": "string", "description": "Reasoning behind the final response"},
16 | "confidence_score": {"type": "number", "description": "Confidence score for the response (0-1)"}
17 | },
18 | "required": ["content", "key_points", "reasoning", "confidence_score"]
19 | }
20 |
21 | with ThreadPoolExecutor(max_workers=num_responses) as executor:
22 | future_to_response = {executor.submit(self._generate_single_response, thoughts, reflection, initial_prompt, research_summary, final_response_schema, i): i for i in range(num_responses)}
23 | final_responses = []
24 | for future in as_completed(future_to_response):
25 | response = future.result()
26 | if response:
27 | final_responses.append(response)
28 |
29 | return final_responses
30 |
31 | def _generate_single_response(self, thoughts: List[Dict[str, Any]], reflection: Dict[str, Any], initial_prompt: str, research_summary: Dict[str, Any], final_response_schema: Dict[str, Any], response_index: int) -> Dict[str, Any]:
32 | final_response_prompt = self._create_final_response_prompt(thoughts, reflection, initial_prompt, research_summary, response_index)
33 |
34 | messages = [
35 | {"role": "system", "content": "You are an AI assistant that provides comprehensive final responses on a topic. Your response should be unique, insightful, and incorporate lessons learned from the thought process, reflection, and research."},
36 | {"role": "user", "content": final_response_prompt}
37 | ]
38 |
39 | try:
40 | response = self.structured_response_generator.generate(messages, final_response_schema)
41 | return response
42 | except Exception as e:
43 | print(f"Error generating final response {response_index + 1}: {e}")
44 | return None
45 |
46 | def _create_final_response_prompt(self, thoughts: List[Dict[str, Any]], reflection: Dict[str, Any], initial_prompt: str, research_summary: Dict[str, Any], response_index: int) -> str:
47 | prompt = f"""Based on the following information, provide a comprehensive final response to the initial prompt: '{initial_prompt}'
48 |
49 | Research Summary:
50 | {research_summary['summary']}
51 |
52 | Key Research Points:
53 | {self._format_list(research_summary['key_points'])}
54 |
55 | Thought Process:
56 | {self._format_thoughts(thoughts)}
57 |
58 | Reflection:
59 | {reflection['content']}
60 |
61 | Reflection Key Points:
62 | {self._format_list(reflection['key_points'])}
63 |
64 | Generate a unique and insightful response (attempt {response_index + 1}) that incorporates the lessons learned from the thought process, reflection, and research. Ensure to include key points in your response, provide reasoning for your conclusions, and assign a confidence score to your response.
65 | """
66 | return prompt
67 |
68 | def _format_thoughts(self, thoughts: List[Dict[str, Any]]) -> str:
69 | formatted_thoughts = ""
70 | for i, thought in enumerate(thoughts, 1):
71 | formatted_thoughts += f"Thought {i}:\n"
72 | formatted_thoughts += f"Content: {thought['content'][:200]}...\n"
73 | formatted_thoughts += f"Key Points: {', '.join(thought['key_points'])}\n\n"
74 | return formatted_thoughts
75 |
76 | def _format_list(self, items: List[str]) -> str:
77 | return "\n".join(f"- {item}" for item in items)
78 |
79 | def choose_best_response(self, final_responses: List[Dict[str, Any]]) -> Dict[str, Any]:
80 | best_response_schema = {
81 | "type": "object",
82 | "properties": {
83 | "chosen_response": {"type": "integer", "description": "The index of the chosen best response (1-based)"},
84 | "reasoning": {"type": "string", "description": "Reasoning for choosing this response"}
85 | },
86 | "required": ["chosen_response", "reasoning"]
87 | }
88 |
89 | choice_prompt = f"""Analyze the following final responses and choose the best one based on its comprehensiveness, insight, reasoning, and overall quality:
90 |
91 | {json.dumps(final_responses, indent=2)}
92 |
93 | Choose the best response and provide reasoning for your choice. Return the index of the chosen response (1-based) and your reasoning.
94 | """
95 |
96 | messages = [
97 | {"role": "system", "content": "You are an AI assistant tasked with choosing the best final response from multiple options. Provide a thorough analysis and justification for your choice."},
98 | {"role": "user", "content": choice_prompt}
99 | ]
100 |
101 | choice = self.structured_response_generator.generate(messages, best_response_schema)
102 | return choice
103 |
104 | def generate_meta_analysis(self, final_responses: List[Dict[str, Any]], best_response: Dict[str, Any]) -> Dict[str, Any]:
105 | meta_analysis_schema = {
106 | "type": "object",
107 | "properties": {
108 | "overall_quality": {"type": "string", "description": "Assessment of the overall quality of the responses"},
109 | "key_insights": {"type": "array", "items": {"type": "string"}, "description": "Key insights from all responses"},
110 | "areas_for_improvement": {"type": "array", "items": {"type": "string"}, "description": "Areas where the responses could be improved"},
111 | "confidence_analysis": {"type": "string", "description": "Analysis of the confidence scores across responses"}
112 | },
113 | "required": ["overall_quality", "key_insights", "areas_for_improvement", "confidence_analysis"]
114 | }
115 |
116 | meta_analysis_prompt = f"""Perform a meta-analysis of the following final responses and the chosen best response:
117 |
118 | Final Responses:
119 | {json.dumps(final_responses, indent=2)}
120 |
121 | Chosen Best Response:
122 | {json.dumps(best_response, indent=2)}
123 |
124 | Provide an assessment of the overall quality of the responses, key insights from all responses, areas for improvement, and an analysis of the confidence scores across responses.
125 | """
126 |
127 | messages = [
128 | {"role": "system", "content": "You are an AI assistant tasked with performing a meta-analysis of multiple responses to a complex question. Provide a thorough and insightful analysis."},
129 | {"role": "user", "content": meta_analysis_prompt}
130 | ]
131 |
132 | meta_analysis = self.structured_response_generator.generate(messages, meta_analysis_schema)
133 | return meta_analysis
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | ### Welcome to the true thinking AI ###
2 | # this model can think and learn from its own thoughts and responses.
3 | # it can also analyze its own thoughts and responses.
4 | # it can also reflect on its own thoughts and responses.
5 | # it can also generate a final thought on a topic.
6 | # it uses groq's API to connect to the openai API.
7 | # you can change the model, the API key, and the base URL to whatever you want.
8 | # the model used here is llama-3.1-70b-versatile, which is a large language model.
9 | # the API key is the key for the groq API. you can get one by signing up at https://console.groq.com/ or change with the base URL of your OpenAI-compatible API.
10 | # the base URL is the base URL for the groq API. you can get one by signing up at https://console.groq.com/ or change with the base URL of your OpenAI-compatible API.
11 | # the output schema is the schema for the response. you can change this to whatever you want.
12 | # the thinking time is the time it takes for the model to think and learn from its own thoughts and responses. you can change this to whatever you want.
13 | # the thinking depth is the depth of the thoughts. you can change this to whatever you want.
14 | # the number of iterations is the number of iterations the model will go through. you can change this to whatever you want.
15 | # the thought depth is the depth of the thoughts. you can change this to whatever you want.
16 | # the response analyzer is the response analyzer. you can change this to whatever you want.
17 |
18 | import os
19 | import openai
20 | from structured_response_generator import StructuredResponseGenerator
21 | from thought_generator import ThoughtGenerator
22 | from response_analyzer import ResponseAnalyzer
23 | from final_response_generator import FinalResponseGenerator
24 | from web_research_and_scraper import WebResearchAndScraper
25 |
26 | # ANSI escape codes for colors
27 | class Colors:
28 | HEADER = '\033[95m'
29 | BLUE = '\033[94m'
30 | CYAN = '\033[96m'
31 | GREEN = '\033[92m'
32 | YELLOW = '\033[93m'
33 | RED = '\033[91m'
34 | ENDC = '\033[0m'
35 | BOLD = '\033[1m'
36 | UNDERLINE = '\033[4m'
37 |
38 | def print_separator():
39 | print("\n" + "="*50 + "\n")
40 |
41 | def print_colored(text, color):
42 | print(f"{color}{text}{Colors.ENDC}")
43 |
44 | if __name__ == "__main__":
45 | client = openai.OpenAI(
46 | base_url="https://api.groq.com/openai/v1",
47 | api_key=os.environ.get("GROQ_API_KEY")
48 | )
49 |
50 | structured_response_generator = StructuredResponseGenerator(client) # this is the model used to generate the responses.
51 | web_researcher = WebResearchAndScraper(structured_response_generator)
52 | thought_generator = ThoughtGenerator(structured_response_generator, web_researcher) # this is the model used to generate the thoughts.
53 | response_analyzer = ResponseAnalyzer(structured_response_generator) # this is the model used to analyze the responses.
54 | final_response_generator = FinalResponseGenerator(structured_response_generator) # this is the model used to generate the final responses.
55 |
56 | initial_prompt = input("Enter the initial prompt: ") # this is the initial prompt for the entire process.
57 |
58 | try:
59 | print("Initiating web research...")
60 | print_separator()
61 | research_summary = web_researcher.conduct_research(initial_prompt)
62 | print("Web research completed.")
63 | print(f"Research summary: {research_summary['summary']}")
64 | print("Key points from research:")
65 | for point in research_summary['key_points']:
66 | print(f"- {point}")
67 | print_separator()
68 |
69 | print("Initiating thought generation process...")
70 | print_separator()
71 |
72 | thoughts, thinking_time = thought_generator.generate_thoughts(initial_prompt, research_summary)
73 |
74 | print(f"Thought generation completed in {thinking_time:.2f} seconds.")
75 | print(f"Generated {len(thoughts)} thoughts.")
76 | print_separator()
77 |
78 | print("Thought Stream:")
79 | for i, thought in enumerate(thoughts, 1):
80 | print(f"\nThought {i}:")
81 | print(f"Content: {thought['content']}")
82 | print("Key Points:")
83 | for point in thought['key_points']:
84 | print(f"- {point}")
85 | print(f"Continue Thinking: {'Yes' if thought['continue_thinking'] else 'No'}")
86 | print(f"Conduct Research: {'Yes' if thought['conduct_research'] else 'No'}")
87 | print(f"Reasoning: {thought['reasoning']}")
88 | print("-" * 30)
89 |
90 | print_separator()
91 | print("Generating comprehensive response...")
92 | response = thought_generator.generate_response(thoughts, initial_prompt)
93 | print("\nComprehensive Response:")
94 | print(f"Content: {response['content']}")
95 | print("Key Points:")
96 | for point in response['key_points']:
97 | print(f"- {point}")
98 | print(f"Reasoning: {response['reasoning']}")
99 |
100 | print_separator()
101 | print("Reflecting on the thought process...")
102 | reflection = thought_generator.reflect(thoughts, response, thinking_time)
103 | print("\nReflection:")
104 | print(f"Content: {reflection['content']}")
105 | print("Key Points:")
106 | for point in reflection['key_points']:
107 | print(f"- {point}")
108 | print("\nAreas for Improvement:")
109 | for area in reflection['areas_for_improvement']:
110 | print(f"- {area}")
111 | print(f"\nConfidence Level: {reflection['confidence_level']:.2f}")
112 | print(f"\nMeta-Cognition: {reflection['meta_cognition']}")
113 |
114 | print_separator()
115 | print_colored("Generating final responses...", Colors.HEADER)
116 | final_responses = final_response_generator.generate_final_responses(thoughts, reflection, initial_prompt, research_summary)
117 | for i, response in enumerate(final_responses, 1):
118 | print_colored(f"\nFinal Response {i}:", Colors.BLUE)
119 | print(f"Content: {response['content']}")
120 | print_colored("Key Points:", Colors.CYAN)
121 | for point in response['key_points']:
122 | print(f"- {point}")
123 | print(f"Reasoning: {response['reasoning']}")
124 | print(f"Confidence Score: {response['confidence_score']:.2f}")
125 | print("-" * 30)
126 |
127 | print_separator()
128 | print_colored("Choosing the best final response...", Colors.HEADER)
129 | best_response_choice = final_response_generator.choose_best_response(final_responses)
130 | best_response = final_responses[best_response_choice['chosen_response'] - 1]
131 |
132 | print_separator()
133 | print_colored("Generating meta-analysis...", Colors.HEADER)
134 | meta_analysis = final_response_generator.generate_meta_analysis(final_responses, best_response_choice)
135 | print_colored("\nMeta-Analysis:", Colors.YELLOW)
136 | print(f"Overall Quality: {meta_analysis['overall_quality']}")
137 | print_colored("Key Insights:", Colors.CYAN)
138 | for insight in meta_analysis['key_insights']:
139 | print(f"- {insight}")
140 | print_colored("Areas for Improvement:", Colors.CYAN)
141 | for area in meta_analysis['areas_for_improvement']:
142 | print(f"- {area}")
143 | print(f"Confidence Analysis: {meta_analysis['confidence_analysis']}")
144 |
145 | # Color-coded final response as the last output
146 | print_separator()
147 | print_colored("FINAL RESPONSE:", Colors.BOLD + Colors.GREEN)
148 | print_colored(f"Content: {best_response['content']}", Colors.GREEN)
149 | print_colored("Key Points:", Colors.CYAN)
150 | for point in best_response['key_points']:
151 | print_colored(f"- {point}", Colors.CYAN)
152 | print_colored(f"Reasoning: {best_response['reasoning']}", Colors.YELLOW)
153 | print_colored(f"Confidence Score: {best_response['confidence_score']:.2f}", Colors.YELLOW)
154 | print_colored(f"\nReasoning for choice: {best_response_choice['reasoning']}", Colors.YELLOW)
155 |
156 | except Exception as e:
157 | print_colored(f"An error occurred: {e}", Colors.RED)
158 | print("Debug information:")
159 | print(f"Thoughts: {thoughts}")
160 | print(f"Initial prompt: {initial_prompt}")
--------------------------------------------------------------------------------
/response_analyzer.py:
--------------------------------------------------------------------------------
1 | import json
2 |
3 | class ResponseAnalyzer:
4 | def __init__(self, structured_response_generator):
5 | self.structured_response_generator = structured_response_generator
6 |
7 | def analyze_thoughts(self, initial_prompt, all_thoughts, output_schema):
8 | analysis_prompt = f"""Analyze the following thoughts on the initial prompt: '{initial_prompt}'
9 |
10 | Thoughts:
11 | {self._format_thoughts(all_thoughts)}
12 |
13 | Provide a comprehensive analysis of these thoughts, highlighting key trends, insights, and potential implications.
14 | Your response should follow the specified output format and ensure proper JSON formatting."""
15 |
16 | messages = [
17 | {"role": "system", "content": "You are an AI assistant specialized in analyzing and synthesizing complex ideas. Ensure your output follows the specified JSON format and uses proper JSON formatting."},
18 | {"role": "user", "content": analysis_prompt}
19 | ]
20 |
21 | analysis = self.structured_response_generator.generate(messages, output_schema)
22 | analysis['type'] = 'analysis'
23 | return analysis
24 |
25 | def _format_thoughts(self, thoughts):
26 | formatted_thoughts = ""
27 | for i, thought in enumerate(thoughts, 1):
28 | formatted_thoughts += f"Thought {i} ({thought['type']}):\n"
29 | formatted_thoughts += f"Content: {thought['content'][:200]}...\n"
30 | formatted_thoughts += f"Key Points: {', '.join(thought['key_points'])}\n\n"
31 | return formatted_thoughts
--------------------------------------------------------------------------------
/simple_thought_loop_initial_concept.py:
--------------------------------------------------------------------------------
1 | import os
2 | import time
3 | import random
4 | from groq import Groq
5 |
6 | def generate_groq_response(messages, model="llama3-8b-8192"):
7 | client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
8 | chat_completion = client.chat.completions.create(
9 | messages=messages,
10 | model=model,
11 | )
12 | return chat_completion.choices[0].message.content
13 |
14 | def generate_thoughts(initial_prompt, min_time=5, max_time=30):
15 | messages = [
16 | {"role": "system", "content": "You are an AI assistant capable of deep, iterative thinking. Generate comprehensive thoughts and review them critically."},
17 | {"role": "user", "content": initial_prompt}
18 | ]
19 |
20 | start_time = time.time()
21 | elapsed_time = 0
22 | thoughts = []
23 |
24 | while elapsed_time < max_time:
25 | response = generate_groq_response(messages)
26 | thoughts.append(response)
27 |
28 | messages.append({"role": "assistant", "content": response})
29 | messages.append({"role": "user", "content": "Continue this line of thinking. Expand on the previous thoughts and explore new angles."})
30 |
31 | elapsed_time = time.time() - start_time
32 | if elapsed_time >= min_time:
33 | break
34 |
35 | time.sleep(random.uniform(1, 3)) # Random pause between thoughts
36 |
37 | review_prompt = f"Review and critically analyze the following thoughts on the initial prompt '{initial_prompt}':\n\n"
38 | for i, thought in enumerate(thoughts, 1):
39 | review_prompt += f"Thought {i}: {thought}\n\n"
40 | review_prompt += "Based on this review, provide a final, comprehensive response that synthesizes and expands upon these thoughts."
41 |
42 | messages.append({"role": "user", "content": review_prompt})
43 | final_response = generate_groq_response(messages)
44 |
45 | return final_response, elapsed_time, thoughts
46 |
47 | if __name__ == "__main__":
48 | initial_prompt = "Explore the potential long-term impacts of artificial general intelligence on human society."
49 |
50 | final_response, thinking_time, all_thoughts = generate_thoughts(initial_prompt)
51 |
52 | print(f"Thought for {thinking_time:.2f} seconds")
53 | print("\nThinking process:")
54 | for i, thought in enumerate(all_thoughts, 1):
55 | print(f"Thought {i}: {thought[:100]}...") # Print first 100 characters of each thought
56 | print("\nFinal response:")
57 | print(final_response)
58 |
--------------------------------------------------------------------------------
/structured_response_generator.py:
--------------------------------------------------------------------------------
1 | import json
2 | import time
3 | import openai
4 |
5 | class StructuredResponseGenerator:
6 | def __init__(self, client, model="llama-3.1-70b-versatile"):
7 | self.client = client
8 | self.model = model
9 |
10 | def generate(self, messages, output_schema, max_retries=3):
11 | for attempt in range(max_retries):
12 | try:
13 | response = self.client.chat.completions.create(
14 | model=self.model,
15 | messages=messages + [
16 | {"role": "system", "content": f"Respond with a JSON object that follows this schema: {json.dumps(output_schema)}"}
17 | ],
18 | response_format={"type": "json_object"},
19 | )
20 | content = response.choices[0].message.content
21 | parsed_content = json.loads(content)
22 |
23 | # Validate the response against the schema
24 | if self._validate_schema(parsed_content, output_schema):
25 | return parsed_content
26 | else:
27 | raise ValueError("Response does not match the required schema")
28 |
29 | except (openai.BadRequestError, json.JSONDecodeError, ValueError) as e:
30 | if attempt == max_retries - 1:
31 | raise
32 | print(f"Error occurred: {e}. Retrying...")
33 |
34 | # Add feedback to the messages
35 | messages.append({"role": "system", "content": f"The previous response was invalid. Please ensure your response strictly adheres to the following JSON schema: {json.dumps(output_schema)}"})
36 |
37 | time.sleep(1)
38 |
39 | def _validate_schema(self, content, schema):
40 | required_properties = schema.get('properties', {}).keys()
41 | return all(prop in content for prop in required_properties)
--------------------------------------------------------------------------------
/thought_generator.py:
--------------------------------------------------------------------------------
1 | import time
2 | import random
3 | from typing import List, Dict, Any, Tuple
4 |
5 | class ThoughtGenerator:
6 | def __init__(self, structured_response_generator, web_researcher):
7 | self.structured_response_generator = structured_response_generator
8 | self.web_researcher = web_researcher
9 |
10 | def generate_thoughts(self, initial_prompt: str, initial_research_summary: Dict[str, Any], max_thoughts: int = 20, max_thinking_time: float = 300) -> Tuple[List[Dict[str, Any]], float]:
11 | thoughts = []
12 | start_time = time.time()
13 |
14 | thought_schema = {
15 | "type": "object",
16 | "properties": {
17 | "content": {"type": "string", "description": "The main content of the thought"},
18 | "key_points": {"type": "array", "items": {"type": "string"}, "description": "Key points from the thought"},
19 | "reasoning": {"type": "string", "description": "Detailed reasoning behind the thought"},
20 | "continue_thinking": {"type": "boolean", "description": "Whether to continue the thought process"},
21 | "conduct_research": {"type": "boolean", "description": "Whether additional research is needed"}
22 | },
23 | "required": ["content", "key_points", "reasoning", "continue_thinking", "conduct_research"]
24 | }
25 |
26 | current_research_summary = initial_research_summary
27 |
28 | while len(thoughts) < max_thoughts and (time.time() - start_time) < max_thinking_time:
29 | thought_prompt = self._create_thought_prompt(initial_prompt, current_research_summary, thoughts)
30 |
31 | messages = [
32 | {"role": "system", "content": "You are an AI assistant generating deep, insightful thoughts on a given topic. Provide thorough reasoning and decide if more thinking or research is needed."},
33 | {"role": "user", "content": thought_prompt}
34 | ]
35 |
36 | try:
37 | thought = self.structured_response_generator.generate(messages, thought_schema)
38 | thoughts.append(thought)
39 |
40 | if thought['conduct_research']:
41 | research_query = self._generate_research_query(initial_prompt, thoughts)
42 | new_research = self.web_researcher.conduct_research(research_query)
43 | current_research_summary = self._merge_research(current_research_summary, new_research)
44 |
45 | if not thought['continue_thinking']:
46 | break
47 | except Exception as e:
48 | print(f"Error generating thought {len(thoughts) + 1}: {e}")
49 | break
50 |
51 | thinking_time = time.time() - start_time
52 | return thoughts, thinking_time
53 |
54 | def _create_thought_prompt(self, initial_prompt: str, research_summary: Dict[str, Any], previous_thoughts: List[Dict[str, Any]]) -> str:
55 | prompt = f"Initial prompt: {initial_prompt}\n\nCurrent research summary: {research_summary['summary']}\n\n"
56 |
57 | if previous_thoughts:
58 | prompt += "Previous thoughts:\n"
59 | for i, thought in enumerate(previous_thoughts, 1):
60 | prompt += f"Thought {i}:\nContent: {thought['content']}\nReasoning: {thought['reasoning']}\n\n"
61 |
62 | prompt += "\nGenerate the next thought in this sequence. Provide detailed reasoning for your thought. Decide if more thinking is needed (continue_thinking) and if additional research would be beneficial (conduct_research)."
63 | return prompt
64 |
65 | def _generate_research_query(self, initial_prompt: str, thoughts: List[Dict[str, Any]]) -> str:
66 | query_schema = {
67 | "type": "object",
68 | "properties": {
69 | "query": {"type": "string", "description": "A focused research query based on the current thoughts and initial prompt"}
70 | },
71 | "required": ["query"]
72 | }
73 |
74 | query_prompt = f"Based on the initial prompt: '{initial_prompt}' and the following thoughts:\n"
75 | for i, thought in enumerate(thoughts[-3:], 1): # Consider only the last 3 thoughts for brevity
76 | query_prompt += f"Thought {i}: {thought['content']}\n"
77 | query_prompt += "\nGenerate a focused research query to gather more information on the most pressing aspect of the current thought process."
78 |
79 | messages = [
80 | {"role": "system", "content": "You are an AI assistant generating a focused research query based on the current thought process."},
81 | {"role": "user", "content": query_prompt}
82 | ]
83 |
84 | try:
85 | query_response = self.structured_response_generator.generate(messages, query_schema)
86 | return query_response['query']
87 | except Exception as e:
88 | print(f"Error generating research query: {e}")
89 | return initial_prompt # Fallback to the initial prompt if query generation fails
90 |
91 | def _merge_research(self, original_research: Dict[str, Any], new_research: Dict[str, Any]) -> Dict[str, Any]:
92 | merged_summary = f"{original_research['summary']}\n\nAdditional research:\n{new_research['summary']}"
93 | merged_key_points = list(set(original_research['key_points'] + new_research['key_points']))
94 | merged_sources = list(set(original_research.get('sources', []) + new_research.get('sources', [])))
95 |
96 | return {
97 | "summary": merged_summary,
98 | "key_points": merged_key_points,
99 | "sources": merged_sources
100 | }
101 |
102 | def generate_response(self, thoughts: List[Dict[str, Any]], initial_prompt: str) -> Dict[str, Any]:
103 | response_schema = {
104 | "type": "object",
105 | "properties": {
106 | "content": {"type": "string", "description": "A comprehensive response based on the thoughts"},
107 | "key_points": {"type": "array", "items": {"type": "string"}, "description": "Key points from the response"},
108 | "reasoning": {"type": "string", "description": "Detailed reasoning behind the response"}
109 | },
110 | "required": ["content", "key_points", "reasoning"]
111 | }
112 |
113 | response_prompt = self._create_response_prompt(thoughts, initial_prompt)
114 |
115 | messages = [
116 | {"role": "system", "content": "You are an AI assistant generating a comprehensive response based on a series of thoughts. Synthesize the information, provide a coherent answer, and include detailed reasoning."},
117 | {"role": "user", "content": response_prompt}
118 | ]
119 |
120 | try:
121 | response = self.structured_response_generator.generate(messages, response_schema)
122 | return response
123 | except Exception as e:
124 | print(f"Error generating response: {e}")
125 | return {"content": "Error generating response", "key_points": [], "reasoning": ""}
126 |
127 | def _create_response_prompt(self, thoughts: List[Dict[str, Any]], initial_prompt: str) -> str:
128 | prompt = f"Initial prompt: {initial_prompt}\n\nThoughts:\n"
129 | for i, thought in enumerate(thoughts, 1):
130 | prompt += f"Thought {i}:\nContent: {thought['content']}\nReasoning: {thought['reasoning']}\n\n"
131 | prompt += "\nBased on these thoughts, generate a comprehensive response to the initial prompt. Include detailed reasoning for your response."
132 | return prompt
133 |
134 | def reflect(self, thoughts: List[Dict[str, Any]], response: Dict[str, Any], thinking_time: float) -> Dict[str, Any]:
135 | reflection_schema = {
136 | "type": "object",
137 | "properties": {
138 | "content": {"type": "string", "description": "A reflection on the thought process and response"},
139 | "key_points": {"type": "array", "items": {"type": "string"}, "description": "Key points from the reflection"},
140 | "areas_for_improvement": {"type": "array", "items": {"type": "string"}, "description": "Areas where the thought process or response could be improved"},
141 | "confidence_level": {"type": "number", "description": "Confidence level in the overall process and response (0-1)"},
142 | "meta_cognition": {"type": "string", "description": "Analysis of the thinking process itself"}
143 | },
144 | "required": ["content", "key_points", "areas_for_improvement", "confidence_level", "meta_cognition"]
145 | }
146 |
147 | reflection_prompt = self._create_reflection_prompt(thoughts, response, thinking_time)
148 |
149 | messages = [
150 | {"role": "system", "content": "You are an AI assistant reflecting on a thought process and response. Provide deep insights on the process, suggest improvements, and analyze the thinking process itself."},
151 | {"role": "user", "content": reflection_prompt}
152 | ]
153 |
154 | try:
155 | reflection = self.structured_response_generator.generate(messages, reflection_schema)
156 | return reflection
157 | except Exception as e:
158 | print(f"Error generating reflection: {e}")
159 | return {"content": "Error generating reflection", "key_points": [], "areas_for_improvement": [], "confidence_level": 0, "meta_cognition": ""}
160 |
161 | def _create_reflection_prompt(self, thoughts: List[Dict[str, Any]], response: Dict[str, Any], thinking_time: float) -> str:
162 | prompt = f"Thought process:\n"
163 | for i, thought in enumerate(thoughts, 1):
164 | prompt += f"Thought {i}:\nContent: {thought['content']}\nReasoning: {thought['reasoning']}\n\n"
165 | prompt += f"\nFinal Response:\nContent: {response['content']}\nReasoning: {response['reasoning']}\n"
166 | prompt += f"\nThinking time: {thinking_time:.2f} seconds\n"
167 | prompt += "\nReflect on the thought process and response. Identify strengths, weaknesses, and areas for improvement. Analyze the thinking process itself (meta-cognition). Assign a confidence level to the overall process and response."
168 | return prompt
--------------------------------------------------------------------------------
/web_research_and_scraper.py:
--------------------------------------------------------------------------------
1 | import json
2 | import requests
3 | import hashlib
4 | import os
5 | import time
6 | from bs4 import BeautifulSoup
7 | from selenium import webdriver
8 | from selenium.webdriver.chrome.options import Options
9 | from selenium.webdriver.common.by import By
10 | from selenium.webdriver.support.ui import WebDriverWait
11 | from selenium.webdriver.support import expected_conditions as EC
12 | from concurrent.futures import ThreadPoolExecutor, as_completed
13 | from urllib.parse import urlparse
14 | from fake_useragent import UserAgent
15 | import trafilatura
16 |
17 | class WebResearchAndScraper:
18 | def __init__(self, structured_response_generator, cache_dir='./cache', max_retries=3, timeout=30):
19 | self.structured_response_generator = structured_response_generator
20 | self.chrome_options = Options()
21 | self.chrome_options.add_argument("--headless")
22 | self.chrome_options.add_argument("--disable-gpu")
23 | self.chrome_options.add_argument("--no-sandbox")
24 | self.chrome_options.add_argument("--disable-dev-shm-usage")
25 | self.cache_dir = cache_dir
26 | self.max_retries = max_retries
27 | self.timeout = timeout
28 | self.user_agent = UserAgent()
29 | os.makedirs(self.cache_dir, exist_ok=True)
30 |
31 | def generate_search_terms(self, topic):
32 | search_terms_schema = {
33 | "type": "object",
34 | "properties": {
35 | "search_terms": {
36 | "type": "array",
37 | "items": {"type": "string"},
38 | "description": "A list of search terms related to the topic"
39 | }
40 | },
41 | "required": ["search_terms"]
42 | }
43 |
44 | messages = [
45 | {"role": "system", "content": "You are an AI assistant that generates relevant and diverse search terms for a given topic."},
46 | {"role": "user", "content": f"Generate a list of 5 search terms for the topic: {topic}. Include a mix of broad and specific terms."}
47 | ]
48 | print("Generating search terms...")
49 | response = self.structured_response_generator.generate(messages, search_terms_schema)
50 | print("Search terms generated.")
51 | return response['search_terms']
52 |
53 | def search_and_scrape(self, search_terms, num_results=5):
54 | all_content = []
55 | print("Searching and scraping...")
56 |
57 | with ThreadPoolExecutor(max_workers=len(search_terms) * 3) as executor:
58 | future_to_url = {}
59 | for term in search_terms:
60 | future_to_url.update(self._search_google(term, num_results, executor))
61 | future_to_url.update(self._search_bing(term, num_results, executor))
62 | future_to_url.update(self._search_duckduckgo(term, num_results, executor))
63 |
64 | for future in as_completed(future_to_url):
65 | url = future_to_url[future]
66 | try:
67 | content = future.result()
68 | if content:
69 | all_content.append(content)
70 | except Exception as e:
71 | print(f"Error scraping {url}: {e}")
72 |
73 | print("All content scraped.")
74 | return all_content
75 |
76 | def _search_google(self, query, num_results, executor):
77 | url = f"https://www.google.com/search?q={query}&num={num_results}"
78 | return self._search_engine(url, 'div', 'yuRUbf', executor)
79 |
80 | def _search_bing(self, query, num_results, executor):
81 | url = f"https://www.bing.com/search?q={query}&count={num_results}"
82 | return self._search_engine(url, 'li', 'b_algo', executor)
83 |
84 | def _search_duckduckgo(self, query, num_results, executor):
85 | url = f"https://html.duckduckgo.com/html/?q={query}"
86 | return self._search_engine(url, 'div', 'links_main', executor)
87 |
88 | def _search_engine(self, url, tag, class_name, executor):
89 | for _ in range(self.max_retries):
90 | try:
91 | headers = {'User-Agent': self.user_agent.random}
92 | response = requests.get(url, headers=headers, timeout=self.timeout)
93 | soup = BeautifulSoup(response.text, 'html.parser')
94 | links = [a.find('a')['href'] for a in soup.find_all(tag, class_=class_name) if a.find('a')]
95 | return {executor.submit(self._scrape_website, link): link for link in links[:5]} # Limit to top 5 results
96 | except Exception as e:
97 | print(f"Error searching {url}: {e}")
98 | time.sleep(1)
99 | return {}
100 |
101 | def _scrape_website(self, url):
102 | cache_key = self._get_cache_key(url)
103 | cached_content = self._get_cached_content(cache_key)
104 | if cached_content:
105 | return cached_content
106 |
107 | for _ in range(self.max_retries):
108 | try:
109 | downloaded = trafilatura.fetch_url(url)
110 | if downloaded:
111 | content = trafilatura.extract(downloaded, include_links=True, include_images=False, include_tables=False)
112 | if content:
113 | cleaned_text = ' '.join(content.split()) # Remove extra whitespace
114 | final_content = f"Source: {url}\n\n{cleaned_text[:2000]}" # Include source URL and limit to 2000 characters
115 | self._cache_content(cache_key, final_content)
116 | return final_content
117 | else:
118 | print(f"No content extracted from {url}")
119 | else:
120 | print(f"Failed to download {url}")
121 | except Exception as e:
122 | print(f"Error scraping {url}: {e}")
123 | time.sleep(1)
124 | return None
125 |
126 | def _get_cache_key(self, url):
127 | return hashlib.md5(url.encode()).hexdigest()
128 |
129 | def _get_cached_content(self, cache_key):
130 | cache_file = os.path.join(self.cache_dir, f"{cache_key}.txt")
131 | if os.path.exists(cache_file):
132 | with open(cache_file, 'r', encoding='utf-8') as f:
133 | return f.read()
134 | return None
135 |
136 | def _cache_content(self, cache_key, content):
137 | cache_file = os.path.join(self.cache_dir, f"{cache_key}.txt")
138 | with open(cache_file, 'w', encoding='utf-8') as f:
139 | f.write(content)
140 |
141 | def summarize_research(self, content):
142 | summary_schema = {
143 | "type": "object",
144 | "properties": {
145 | "summary": {"type": "string", "description": "A concise summary of the research findings"},
146 | "key_points": {
147 | "type": "array",
148 | "items": {"type": "string"},
149 | "description": "Key points extracted from the research"
150 | },
151 | "sources": {
152 | "type": "array",
153 | "items": {"type": "string"},
154 | "description": "List of sources used in the research"
155 | }
156 | },
157 | "required": ["summary", "key_points", "sources"]
158 | }
159 |
160 | messages = [
161 | {"role": "system", "content": "You are an AI assistant that summarizes web research findings comprehensively and accurately."},
162 | {"role": "user", "content": f"Summarize the following web research content, including key points and a list of sources:\n\n{' '.join(content)}"}
163 | ]
164 |
165 | return self.structured_response_generator.generate(messages, summary_schema)
166 |
167 | def conduct_research(self, topic):
168 | search_terms = self.generate_search_terms(topic)
169 | content = self.search_and_scrape(search_terms)
170 | if not content:
171 | return {"summary": "No content found", "key_points": [], "sources": []}
172 | return self.summarize_research(content)
--------------------------------------------------------------------------------