Charles Azam commited on
Commit
7e778b9
·
1 Parent(s): 50658f4

feat: no need to rewrite the async function to sync

Browse files
tests/webcrawler/test_sync_search_speed.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from deepengineer.webcrawler.async_search import linkup_search_async, SearchResponse, SearchResult, ScientificDomains
2
+ from linkup import LinkupClient, LinkupSourcedAnswer
3
+ from typing import Literal
4
+ import time
5
+ import asyncio
6
+
7
+ def _linkup_search_sync(
8
+ search_query: str,
9
+ depth: Literal["standard", "deep"] = "standard",
10
+ output_type: Literal['searchResults', 'sourcedAnswer', 'structured'] = "sourcedAnswer",
11
+ include_images: bool = False,
12
+ include_domains: list[ScientificDomains] = None,
13
+
14
+ ) -> SearchResponse:
15
+ client = LinkupClient()
16
+ search_response: LinkupSourcedAnswer = client.search(
17
+ query=search_query,
18
+ depth=depth,
19
+ output_type=output_type,
20
+ include_images=include_images,
21
+ include_domains=include_domains,
22
+ )
23
+
24
+ search_results = [
25
+ SearchResult(
26
+ title=result.name,
27
+ url=result.url,
28
+ content=result.snippet,
29
+ raw_content=None,
30
+ )
31
+ for result in search_response.sources
32
+ ]
33
+
34
+ # Convert to our Pydantic models
35
+ responses: SearchResponse = SearchResponse(
36
+ query=search_query,
37
+ answer=search_response.answer,
38
+ search_results=search_results
39
+ )
40
+ return responses
41
+
42
+ def linkup_search_speed_test():
43
+
44
+ """
45
+
46
+ Conclusion: no need to rewrite the async version to sync version. It takes roughly 6 seconds in both cases
47
+ """
48
+
49
+ print("Testing linkup search speed asynchronously...")
50
+ start_time = time.time()
51
+ for i in range(5):
52
+ start_loop_time = time.time()
53
+ output = asyncio.run(linkup_search_async(
54
+ search_query="Would it be possible to make a thermal reactor with graphite and lead?",
55
+ ))
56
+ print(output.answer[:10])
57
+ end_loop_time = time.time()
58
+ print(f"Time taken for loop {i}: {end_loop_time - start_loop_time} seconds")
59
+
60
+
61
+ print("Testing linkup search speed syncronoulsy...")
62
+ start_time = time.time()
63
+ for i in range(5):
64
+ start_loop_time = time.time()
65
+ _linkup_search_sync(
66
+ search_query="Would it be possible to make a thermal reactor with graphite and lead?",
67
+ )
68
+ end_loop_time = time.time()
69
+ print(f"Time taken for loop {i}: {end_loop_time - start_loop_time} seconds")
70
+
71
+ end_time = time.time()
72
+ print(f"Total time taken: {end_time - start_time} seconds")
73
+
74
+
75
+