parent
							
								
									2cf86acd6d
								
							
						
					
					
						commit
						36492c1e0f
					
				| @ -0,0 +1,88 @@ | |||||||
|  | """ | ||||||
|  | pip3 install -U swarms | ||||||
|  | pip3 install -U chromadb | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | task -> Understanding Agent [understands the problem better] -> Summarize of the conversation -> research agent that has access to internt perplexity -> final rag agent  | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Todo | ||||||
|  | - Use better llm -- gpt4, claude, gemini | ||||||
|  | - Make better system prompt | ||||||
|  | - Populate the vector database with q/a of past history | ||||||
|  | """ | ||||||
|  | 
 | ||||||
|  | from swarms import Agent, llama3Hosted, AgentRearrange | ||||||
|  | from pydantic import BaseModel | ||||||
|  | from playground.memory.chromadb_example import ChromaDB | ||||||
|  | 
 | ||||||
|  | # Initialize the language model agent (e.g., GPT-3) | ||||||
|  | llm = llama3Hosted(max_tokens=3000) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Initialize Memory | ||||||
|  | memory = ChromaDB(output_dir="swarm_mechanic", n_results=2, verbose=True) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Perplexity Agent | ||||||
|  | # def webbrowser(query: str): | ||||||
|  | #     # put your logic here | ||||||
|  | #     return query | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Output | ||||||
|  | class EvaluatorOuputSchema(BaseModel): | ||||||
|  |     evaluation: str = None | ||||||
|  |     question_for_user: str = None | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Initialize agents for individual tasks | ||||||
|  | agent1 = Agent( | ||||||
|  |     agent_name="Summary ++ Hightlighter Agent", | ||||||
|  |     system_prompt="Generate a simple, direct, and reliable summary of the input task alongside the highlights", | ||||||
|  |     llm=llm, | ||||||
|  |     max_loops=1, | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | # Point out that if their are details that can be added | ||||||
|  | # What do you mean? What lights do you have turned on. | ||||||
|  | agent2 = Agent( | ||||||
|  |     agent_name="Evaluator", | ||||||
|  |     system_prompt="Summarize and evaluate the summary and the users demand, always be interested in learning more about the situation with extreme precision.", | ||||||
|  |     llm=llm, | ||||||
|  |     max_loops=1, | ||||||
|  |     list_base_models=[EvaluatorOuputSchema], | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | # research_agent = Agent( | ||||||
|  | #     agent_name="Research Agent", | ||||||
|  | #     system_prompt="Summarize and evaluate the summary and the users demand, always be interested in learning more about the situation with extreme precision.", | ||||||
|  | #     llm=llm, | ||||||
|  | #     max_loops=1, | ||||||
|  | #     tool = [webbrowser] | ||||||
|  | # ) | ||||||
|  | 
 | ||||||
|  | agent3 = Agent( | ||||||
|  |     agent_name="Summarizer Agent", | ||||||
|  |     system_prompt="Summarize the entire history of the interaction", | ||||||
|  |     llm=llm, | ||||||
|  |     max_loops=1, | ||||||
|  |     long_term_memory=memory, | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Task | ||||||
|  | task = "Car Model: S-Class, Car Year: 2020, Car Mileage: 10000, all my service lights are on, what should i do?" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Swarm | ||||||
|  | swarm = AgentRearrange( | ||||||
|  |     agents=[agent1, agent2, agent3], | ||||||
|  |     flow=f"{agent1.agent_name} -> {agent2.agent_name} -> {agent3.agent_name}", | ||||||
|  |     memory_system=memory, | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | # Task | ||||||
|  | out = swarm.run(task) | ||||||
|  | print(out) | ||||||
| @ -1,15 +0,0 @@ | |||||||
| from swarms.memory import ChromaDB |  | ||||||
| 
 |  | ||||||
| # Initialize the memory |  | ||||||
| chroma = ChromaDB( |  | ||||||
|     metric="cosine", |  | ||||||
|     limit_tokens=1000, |  | ||||||
|     verbose=True, |  | ||||||
| ) |  | ||||||
| 
 |  | ||||||
| # Add text |  | ||||||
| text = "This is a test" |  | ||||||
| chroma.add(text) |  | ||||||
| 
 |  | ||||||
| # Search for similar text |  | ||||||
| similar_text = chroma.query(text) |  | ||||||
| @ -0,0 +1,73 @@ | |||||||
|  | import timeit | ||||||
|  | from typing import Callable, Iterable, List, Optional, TypeVar | ||||||
|  | 
 | ||||||
|  | T = TypeVar("T") | ||||||
|  | R = TypeVar("R") | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def optimized_loop( | ||||||
|  |     data: Iterable[T], | ||||||
|  |     operation: Callable[[T], R], | ||||||
|  |     condition: Optional[Callable[[T], bool]] = None, | ||||||
|  | ) -> List[R]: | ||||||
|  |     """ | ||||||
|  |     Perform an optimized loop over the input data, applying an operation to each element. | ||||||
|  |     Optionally, filter elements based on a condition before applying the operation. | ||||||
|  | 
 | ||||||
|  |     Args: | ||||||
|  |         data (Iterable[T]): The input data to be processed. Can be any iterable type. | ||||||
|  |         operation (Callable[[T], R]): The operation to be applied to each element. | ||||||
|  |         condition (Optional[Callable[[T], bool]]): An optional condition to filter elements before applying the operation. | ||||||
|  | 
 | ||||||
|  |     Returns: | ||||||
|  |         List[R]: The result of applying the operation to the filtered elements. | ||||||
|  |     """ | ||||||
|  |     if condition is not None: | ||||||
|  |         return [operation(x) for x in data if condition(x)] | ||||||
|  |     else: | ||||||
|  |         return [operation(x) for x in data] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Sample data, operation, and condition for benchmarking | ||||||
|  | data = list(range(1000000)) | ||||||
|  | operation = lambda x: x * x | ||||||
|  | condition = lambda x: x % 2 == 0 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Define a traditional loop for comparison | ||||||
|  | def traditional_loop(data: Iterable[int]) -> List[int]: | ||||||
|  |     result = [] | ||||||
|  |     for x in data: | ||||||
|  |         if x % 2 == 0: | ||||||
|  |             result.append(x * x) | ||||||
|  |     return result | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Define a benchmarking function | ||||||
|  | def benchmark(): | ||||||
|  |     # Time the execution of the optimized loop | ||||||
|  |     optimized_time = timeit.timeit( | ||||||
|  |         stmt="optimized_loop(data, operation, condition)", | ||||||
|  |         setup="from __main__ import optimized_loop, data, operation, condition", | ||||||
|  |         globals=globals(), | ||||||
|  |         number=10, | ||||||
|  |     ) | ||||||
|  | 
 | ||||||
|  |     print(f"Optimized loop execution time: {optimized_time:.4f} seconds") | ||||||
|  | 
 | ||||||
|  |     # Time the execution of the traditional loop for comparison | ||||||
|  |     traditional_time = timeit.timeit( | ||||||
|  |         stmt="traditional_loop(data)", | ||||||
|  |         setup="from __main__ import traditional_loop, data", | ||||||
|  |         globals=globals(), | ||||||
|  |         number=10, | ||||||
|  |     ) | ||||||
|  | 
 | ||||||
|  |     print( | ||||||
|  |         f"Traditional loop execution time: {traditional_time:.4f} seconds" | ||||||
|  |     ) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | # Run the benchmark | ||||||
|  | if __name__ == "__main__": | ||||||
|  |     benchmark() | ||||||
					Loading…
					
					
				
		Reference in new issue