langroid 0.1.139__py3-none-any.whl → 0.1.219__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. langroid/__init__.py +70 -0
  2. langroid/agent/__init__.py +22 -0
  3. langroid/agent/base.py +120 -33
  4. langroid/agent/batch.py +134 -35
  5. langroid/agent/callbacks/__init__.py +0 -0
  6. langroid/agent/callbacks/chainlit.py +608 -0
  7. langroid/agent/chat_agent.py +164 -100
  8. langroid/agent/chat_document.py +19 -2
  9. langroid/agent/openai_assistant.py +20 -10
  10. langroid/agent/special/__init__.py +33 -10
  11. langroid/agent/special/doc_chat_agent.py +521 -108
  12. langroid/agent/special/lance_doc_chat_agent.py +258 -0
  13. langroid/agent/special/lance_rag/__init__.py +9 -0
  14. langroid/agent/special/lance_rag/critic_agent.py +136 -0
  15. langroid/agent/special/lance_rag/lance_rag_task.py +80 -0
  16. langroid/agent/special/lance_rag/query_planner_agent.py +180 -0
  17. langroid/agent/special/lance_tools.py +44 -0
  18. langroid/agent/special/neo4j/__init__.py +0 -0
  19. langroid/agent/special/neo4j/csv_kg_chat.py +174 -0
  20. langroid/agent/special/neo4j/neo4j_chat_agent.py +370 -0
  21. langroid/agent/special/neo4j/utils/__init__.py +0 -0
  22. langroid/agent/special/neo4j/utils/system_message.py +46 -0
  23. langroid/agent/special/relevance_extractor_agent.py +23 -7
  24. langroid/agent/special/retriever_agent.py +29 -174
  25. langroid/agent/special/sql/__init__.py +7 -0
  26. langroid/agent/special/sql/sql_chat_agent.py +47 -23
  27. langroid/agent/special/sql/utils/__init__.py +11 -0
  28. langroid/agent/special/sql/utils/description_extractors.py +95 -46
  29. langroid/agent/special/sql/utils/populate_metadata.py +28 -21
  30. langroid/agent/special/table_chat_agent.py +43 -9
  31. langroid/agent/task.py +423 -114
  32. langroid/agent/tool_message.py +67 -10
  33. langroid/agent/tools/__init__.py +8 -0
  34. langroid/agent/tools/duckduckgo_search_tool.py +66 -0
  35. langroid/agent/tools/google_search_tool.py +11 -0
  36. langroid/agent/tools/metaphor_search_tool.py +67 -0
  37. langroid/agent/tools/recipient_tool.py +6 -24
  38. langroid/agent/tools/sciphi_search_rag_tool.py +79 -0
  39. langroid/cachedb/__init__.py +6 -0
  40. langroid/embedding_models/__init__.py +24 -0
  41. langroid/embedding_models/base.py +9 -1
  42. langroid/embedding_models/models.py +117 -17
  43. langroid/embedding_models/protoc/embeddings.proto +19 -0
  44. langroid/embedding_models/protoc/embeddings_pb2.py +33 -0
  45. langroid/embedding_models/protoc/embeddings_pb2.pyi +50 -0
  46. langroid/embedding_models/protoc/embeddings_pb2_grpc.py +79 -0
  47. langroid/embedding_models/remote_embeds.py +153 -0
  48. langroid/language_models/__init__.py +22 -0
  49. langroid/language_models/azure_openai.py +47 -4
  50. langroid/language_models/base.py +26 -10
  51. langroid/language_models/config.py +5 -0
  52. langroid/language_models/openai_gpt.py +407 -121
  53. langroid/language_models/prompt_formatter/__init__.py +9 -0
  54. langroid/language_models/prompt_formatter/base.py +4 -6
  55. langroid/language_models/prompt_formatter/hf_formatter.py +135 -0
  56. langroid/language_models/utils.py +10 -9
  57. langroid/mytypes.py +10 -4
  58. langroid/parsing/__init__.py +33 -1
  59. langroid/parsing/document_parser.py +259 -63
  60. langroid/parsing/image_text.py +32 -0
  61. langroid/parsing/parse_json.py +143 -0
  62. langroid/parsing/parser.py +20 -7
  63. langroid/parsing/repo_loader.py +108 -46
  64. langroid/parsing/search.py +8 -0
  65. langroid/parsing/table_loader.py +44 -0
  66. langroid/parsing/url_loader.py +59 -13
  67. langroid/parsing/urls.py +18 -9
  68. langroid/parsing/utils.py +130 -9
  69. langroid/parsing/web_search.py +73 -0
  70. langroid/prompts/__init__.py +7 -0
  71. langroid/prompts/chat-gpt4-system-prompt.md +68 -0
  72. langroid/prompts/prompts_config.py +1 -1
  73. langroid/utils/__init__.py +10 -0
  74. langroid/utils/algorithms/__init__.py +3 -0
  75. langroid/utils/configuration.py +0 -1
  76. langroid/utils/constants.py +4 -0
  77. langroid/utils/logging.py +2 -5
  78. langroid/utils/output/__init__.py +15 -2
  79. langroid/utils/output/status.py +33 -0
  80. langroid/utils/pandas_utils.py +30 -0
  81. langroid/utils/pydantic_utils.py +446 -4
  82. langroid/utils/system.py +36 -1
  83. langroid/vector_store/__init__.py +34 -2
  84. langroid/vector_store/base.py +33 -2
  85. langroid/vector_store/chromadb.py +42 -13
  86. langroid/vector_store/lancedb.py +226 -60
  87. langroid/vector_store/meilisearch.py +7 -6
  88. langroid/vector_store/momento.py +3 -2
  89. langroid/vector_store/qdrantdb.py +82 -11
  90. {langroid-0.1.139.dist-info → langroid-0.1.219.dist-info}/METADATA +190 -129
  91. langroid-0.1.219.dist-info/RECORD +127 -0
  92. langroid/agent/special/recipient_validator_agent.py +0 -157
  93. langroid/parsing/json.py +0 -64
  94. langroid/utils/web/selenium_login.py +0 -36
  95. langroid-0.1.139.dist-info/RECORD +0 -103
  96. {langroid-0.1.139.dist-info → langroid-0.1.219.dist-info}/LICENSE +0 -0
  97. {langroid-0.1.139.dist-info → langroid-0.1.219.dist-info}/WHEEL +0 -0
@@ -12,6 +12,7 @@ from typing import Dict, List
12
12
  import requests
13
13
  from bs4 import BeautifulSoup
14
14
  from dotenv import load_dotenv
15
+ from duckduckgo_search import DDGS
15
16
  from googleapiclient.discovery import Resource, build
16
17
  from requests.models import Response
17
18
 
@@ -77,3 +78,75 @@ def google_search(query: str, num_results: int = 5) -> List[WebSearchResult]:
77
78
  WebSearchResult(result["title"], result["link"], 3500, 300)
78
79
  for result in raw_results
79
80
  ]
81
+
82
+
83
+ def metaphor_search(query: str, num_results: int = 5) -> List[WebSearchResult]:
84
+ """
85
+ Method that makes an API call by Metaphor client that queries
86
+ the top num_results links that matches the query. Returns a list
87
+ of WebSearchResult objects.
88
+
89
+ Args:
90
+ query (str): The query body that users wants to make.
91
+ num_results (int): Number of top matching results that we want
92
+ to grab
93
+ """
94
+
95
+ load_dotenv()
96
+
97
+ api_key = os.getenv("METAPHOR_API_KEY") or os.getenv("EXA_API_KEY")
98
+ if not api_key:
99
+ raise ValueError(
100
+ """
101
+ Neither METAPHOR_API_KEY nor EXA_API_KEY environment variables are set.
102
+ Please set one of them to your API key, and try again.
103
+ """
104
+ )
105
+
106
+ try:
107
+ from metaphor_python import Metaphor
108
+ except ImportError:
109
+ raise ImportError(
110
+ "You are attempting to use the `metaphor_python` library;"
111
+ "To use it, please install langroid with the `metaphor` extra, e.g. "
112
+ "`pip install langroid[metaphor]` or `poetry add langroid[metaphor]` "
113
+ "(it installs the `metaphor_python` package from pypi)."
114
+ )
115
+
116
+ client = Metaphor(api_key=api_key)
117
+
118
+ response = client.search(
119
+ query=query,
120
+ num_results=num_results,
121
+ )
122
+ raw_results = response.results
123
+
124
+ return [
125
+ WebSearchResult(result.title, result.url, 3500, 300) for result in raw_results
126
+ ]
127
+
128
+
129
+ def duckduckgo_search(query: str, num_results: int = 5) -> List[WebSearchResult]:
130
+ """
131
+ Method that makes an API call by DuckDuckGo client that queries
132
+ the top `num_results` links that matche the query. Returns a list
133
+ of WebSearchResult objects.
134
+
135
+ Args:
136
+ query (str): The query body that users wants to make.
137
+ num_results (int): Number of top matching results that we want
138
+ to grab
139
+ """
140
+
141
+ with DDGS() as ddgs:
142
+ search_results = [r for r in ddgs.text(query, max_results=num_results)]
143
+
144
+ return [
145
+ WebSearchResult(
146
+ title=result["title"],
147
+ link=result["href"],
148
+ max_content_length=3500,
149
+ max_summary_length=300,
150
+ )
151
+ for result in search_results
152
+ ]
@@ -2,3 +2,10 @@ from . import dialog
2
2
  from . import prompts_config
3
3
  from . import templates
4
4
  from . import transforms
5
+
6
+ __all__ = [
7
+ "dialog",
8
+ "prompts_config",
9
+ "templates",
10
+ "transforms",
11
+ ]
@@ -0,0 +1,68 @@
1
+ Image input capabilities: Enabled
2
+
3
+ Tools
4
+ python
5
+ When you send a message containing Python code to python, it will be executed in a
6
+ stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0
7
+ seconds. The drive at '/mnt/data' can be used to save and persist user files. Internet access for this session is disabled. Do not make external web requests or API calls as they will fail.
8
+
9
+ dalle
10
+ // Whenever a description of an image is given, create a prompt that dalle can use to generate the image and abide to the following policy:
11
+ // 1. The prompt must be in English. Translate to English if needed.
12
+ // 2. DO NOT ask for permission to generate the image, just do it!
13
+ // 3. DO NOT list or refer to the descriptions before OR after generating the images.
14
+ // 4. Do not create more than 1 image, even if the user requests more.
15
+ // 5. Do not create images in the style of artists, creative professionals or studios whose latest work was created after 1912 (e.g. Picasso, Kahlo).
16
+ // - You can name artists, creative professionals or studios in prompts only if their latest work was created prior to 1912 (e.g. Van Gogh, Goya)
17
+ // - If asked to generate an image that would violate this policy, instead apply the following procedure: (a) substitute the artist's name with three adjectives that capture key aspects of the style; (b) include an associated artistic movement or era to provide context; and (c) mention the primary medium used by the artist
18
+ // 6. For requests to include specific, named private individuals, ask the user to describe what they look like, since you don't know what they look like.
19
+ // 7. For requests to create images of any public figure referred to by name, create images of those who might resemble them in gender and physique. But they shouldn't look like them. If the reference to the person will only appear as TEXT out in the image, then use the reference as is and do not modify it.
20
+ // 8. Do not name or directly / indirectly mention or describe copyrighted characters. Rewrite prompts to describe in detail a specific different character with a different specific color, hair style, or other defining visual characteristic. Do not discuss copyright policies in responses.
21
+ // The generated prompt sent to dalle should be very detailed, and around 100 words long.
22
+ // Example dalle invocation:
23
+ // // { // "prompt": "<insert prompt here>" // } //
24
+ namespace dalle {
25
+
26
+ // Create images from a text-only prompt.
27
+ type text2im = (_: {
28
+ // The size of the requested image. Use 1024x1024 (square) as the default, 1792x1024 if the user requests a wide image, and 1024x1792 for full-body portraits. Always include this parameter in the request.
29
+ size?: "1792x1024" | "1024x1024" | "1024x1792",
30
+ // The number of images to generate. If the user does not specify a number, generate 1 image.
31
+ n?: number, // default: 2
32
+ // The detailed image description, potentially modified to abide by the dalle policies. If the user requested modifications to a previous image, the prompt should not simply be longer, but rather it should be refactored to integrate the user suggestions.
33
+ prompt: string,
34
+ // If the user references a previous image, this field should be populated with the gen_id from the dalle image metadata.
35
+ referenced_image_ids?: string[],
36
+ }) => any;
37
+
38
+ } // namespace dalle
39
+
40
+ voice_mode
41
+ // Voice mode functions are not available in text conversations.
42
+ namespace voice_mode {
43
+
44
+ } // namespace voice_mode
45
+
46
+ browser
47
+ You have the tool browser. Use browser in the following circumstances:
48
+ - User is asking about current events or something that requires real-time information (weather, sports scores, etc.)
49
+ - User is asking about some term you are totally unfamiliar with (it might be new)
50
+ - User explicitly asks you to browse or provide links to references
51
+
52
+ Given a query that requires retrieval, your turn will consist of three steps:
53
+
54
+ Call the search function to get a list of results.
55
+ Call the mclick function to retrieve a diverse and high-quality subset of these results (in parallel). Remember to SELECT AT LEAST 3 sources when using mclick.
56
+ Write a response to the user based on these results. In your response, cite sources using the citation format below.
57
+ In some cases, you should repeat step 1 twice, if the initial results are unsatisfactory, and you believe that you can refine the query to get better results.
58
+
59
+ You can also open a url directly if one is provided by the user. Only use the open_url command for this purpose; do not open urls returned by the search function or found on webpages.
60
+
61
+ The browser tool has the following commands:
62
+ search(query: str, recency_days: int) Issues a query to a search engine and displays the results.
63
+ mclick(ids: list[str]). Retrieves the contents of the webpages with provided IDs (indices). You should ALWAYS SELECT AT LEAST 3 and at most 10 pages. Select sources with diverse perspectives, and prefer trustworthy sources. Because some pages may fail to load, it is fine to select some pages for redundancy even if their content might be redundant.
64
+ open_url(url: str) Opens the given URL and displays it.
65
+
66
+ For citing quotes from the 'browser' tool: please render in this format: 【{message idx}†{link text}】.
67
+ For long citations: please render in this format: [link text](message idx).
68
+ Otherwise do not render links.
@@ -2,4 +2,4 @@ from pydantic import BaseSettings
2
2
 
3
3
 
4
4
  class PromptsConfig(BaseSettings):
5
- max_tokens: int = 1000
5
+ max_tokens: int = 1000 # for output; NOT USED ANYWHERE
@@ -5,3 +5,13 @@ from . import logging
5
5
  from . import pydantic_utils
6
6
  from . import system
7
7
  from . import output
8
+
9
+ __all__ = [
10
+ "configuration",
11
+ "globals",
12
+ "constants",
13
+ "logging",
14
+ "pydantic_utils",
15
+ "system",
16
+ "output",
17
+ ]
@@ -0,0 +1,3 @@
1
+ from . import graph
2
+
3
+ __all__ = ["graph"]
@@ -17,7 +17,6 @@ class Settings(BaseSettings):
17
17
  cache_type: Literal["redis", "fakeredis", "momento"] = "redis" # cache type
18
18
  interactive: bool = True # interactive mode?
19
19
  gpt3_5: bool = True # use GPT-3.5?
20
- nofunc: bool = False # use model without function_call? (i.e. gpt-4)
21
20
  chat_model: str = "" # language model name, e.g. litellm/ollama/llama2
22
21
  quiet: bool = False # quiet mode (i.e. suppress all output)?
23
22
  notebook: bool = False # running in a notebook?
@@ -16,3 +16,7 @@ class Colors(BaseModel):
16
16
  USER_QUIT = ["q", "x", "quit", "exit", "bye"]
17
17
  NO_ANSWER = "DO-NOT-KNOW"
18
18
  DONE = "DONE"
19
+ PASS = "__PASS__"
20
+ PASS_TO = PASS + ":"
21
+ SEND_TO = "SEND:"
22
+ TOOL = "TOOL"
langroid/utils/logging.py CHANGED
@@ -72,12 +72,9 @@ def setup_file_logger(
72
72
  propagate: bool = False,
73
73
  ) -> logging.Logger:
74
74
  os.makedirs(os.path.dirname(filename), exist_ok=True)
75
- if not append:
76
- if os.path.exists(filename):
77
- os.remove(filename)
78
-
75
+ file_mode = "a" if append else "w"
79
76
  logger = setup_logger(name)
80
- handler = logging.FileHandler(filename)
77
+ handler = logging.FileHandler(filename, mode=file_mode)
81
78
  handler.setLevel(logging.INFO)
82
79
  if log_format:
83
80
  formatter = logging.Formatter(
@@ -1,8 +1,21 @@
1
- import langroid.utils.output.printing as printing
1
+ from . import printing
2
2
 
3
- from langroid.utils.output.printing import (
3
+ from .printing import (
4
4
  shorten_text,
5
5
  print_long_text,
6
6
  show_if_debug,
7
+ SuppressLoggerWarnings,
7
8
  PrintColored,
8
9
  )
10
+
11
+ from .status import status
12
+
13
+ __all__ = [
14
+ "printing",
15
+ "shorten_text",
16
+ "print_long_text",
17
+ "show_if_debug",
18
+ "SuppressLoggerWarnings",
19
+ "PrintColored",
20
+ "status",
21
+ ]
@@ -0,0 +1,33 @@
1
+ import logging
2
+ from contextlib import AbstractContextManager, ExitStack
3
+ from typing import Any
4
+
5
+ from rich.console import Console
6
+
7
+ from langroid.utils.configuration import quiet_mode, settings
8
+
9
+ console = Console()
10
+ logger = logging.getLogger(__name__)
11
+ logger.setLevel(logging.INFO)
12
+
13
+
14
+ def status(
15
+ msg: str,
16
+ log_if_quiet: bool = True,
17
+ ) -> AbstractContextManager[Any]:
18
+ """
19
+ Displays a rich spinner if not in quiet mode, else optionally logs the message.
20
+ """
21
+ stack = ExitStack()
22
+
23
+ if settings.quiet:
24
+ if log_if_quiet:
25
+ logger.info(msg)
26
+ if settings.quiet and log_if_quiet:
27
+ logger.info(msg)
28
+ else:
29
+ stack.enter_context(console.status(msg))
30
+
31
+ stack.enter_context(quiet_mode(not settings.debug))
32
+
33
+ return stack
@@ -0,0 +1,30 @@
1
+ from typing import Any
2
+
3
+ import pandas as pd
4
+
5
+
6
+ def stringify(x: Any) -> str:
7
+ # Convert x to DataFrame if it is not one already
8
+ if isinstance(x, pd.Series):
9
+ df = x.to_frame()
10
+ elif not isinstance(x, pd.DataFrame):
11
+ return str(x)
12
+ else:
13
+ df = x
14
+
15
+ # Truncate long text columns to 1000 characters
16
+ for col in df.columns:
17
+ if df[col].dtype == object:
18
+ df[col] = df[col].apply(
19
+ lambda item: (
20
+ (item[:1000] + "...")
21
+ if isinstance(item, str) and len(item) > 1000
22
+ else item
23
+ )
24
+ )
25
+
26
+ # Limit to 10 rows
27
+ df = df.head(10)
28
+
29
+ # Convert to string
30
+ return df.to_string(index=False) # type: ignore