Skip to content

Commit

Permalink
Merge branch 'main' into opensearch-binary-byte-support
Browse files Browse the repository at this point in the history
  • Loading branch information
logan-markewich authored Jan 23, 2025
2 parents e56a7ca + 2d35a0f commit 5804236
Show file tree
Hide file tree
Showing 29 changed files with 799 additions and 148 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
shell: bash
run: rm -rf llama-index-core/llama_index/core/_static/nltk_cache/corpora/stopwords.zip llama-index-core/llama_index/core/_static/nltk_cache/tokenizers/punkt.zip
- name: Build and publish to pypi
uses: JRubics/poetry-publish@v2.0
uses: JRubics/poetry-publish@v2.1
with:
python_version: ${{ env.PYTHON_VERSION }}
pypi_token: ${{ secrets.LLAMA_INDEX_PYPI_TOKEN }}
Expand Down
4 changes: 4 additions & 0 deletions docs/docs/api_reference/tools/linkup_research.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
::: llama_index.tools.tavily_research
options:
members:
- LinkupToolSpec
52 changes: 34 additions & 18 deletions docs/docs/examples/agent/react_agent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,7 @@
"execution_count": null,
"id": "e8ac1778-0585-43c9-9dad-014d13d7460d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[nltk_data] Downloading package stopwords to /Users/jerryliu/Programmi\n",
"[nltk_data] ng/gpt_index/.venv/lib/python3.10/site-\n",
"[nltk_data] packages/llama_index/legacy/_static/nltk_cache...\n",
"[nltk_data] Unzipping corpora/stopwords.zip.\n",
"[nltk_data] Downloading package punkt to /Users/jerryliu/Programming/g\n",
"[nltk_data] pt_index/.venv/lib/python3.10/site-\n",
"[nltk_data] packages/llama_index/legacy/_static/nltk_cache...\n",
"[nltk_data] Unzipping tokenizers/punkt.zip.\n"
]
}
],
"outputs": [],
"source": [
"from llama_index.core.agent import ReActAgent\n",
"from llama_index.llms.openai import OpenAI\n",
Expand Down Expand Up @@ -468,13 +453,44 @@
"response = agent.chat(\"What is 5+3+2\")\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "76190511-692c-4642-9b86-adac88c98550",
"metadata": {},
"source": [
"### Customizing the Message Role of Observation\n",
"\n",
"If the LLM you use supports function/tool calling, you may set the message role of observations to `MessageRole.TOOL`. \n",
"Doing this will prevent the tool outputs from being misinterpreted as new user messages for some models."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6d5e8c1-c40e-4a96-8d2e-84127f066265",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core.agent import ReActChatFormatter\n",
"from llama_index.core.llms import MessageRole\n",
"\n",
"agent = ReActAgent.from_tools(\n",
" [multiply_tool, add_tool],\n",
" llm=llm,\n",
" react_chat_formatter=ReActChatFormatter.from_defaults(\n",
" observation_role=MessageRole.TOOL\n",
" ),\n",
" verbose=True,\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "llama_index_v2",
"display_name": "LlamaIndex Development",
"language": "python",
"name": "llama_index_v2"
"name": "llama-index-dev"
},
"language_info": {
"codemirror_mode": {
Expand Down
1 change: 1 addition & 0 deletions llama-index-cli/llama_index/cli/upgrade/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@
"AzureSpeechToolSpec": "llama_index.tools.azure_speech",
"WolframAlphaToolSpec": "llama_index.tools.wolfram_alpha",
"SlackToolSpec": "llama_index.tools.slack",
"LinkupToolSpec": "llama_index.tools.linkup_research",
"TavilyToolSpec": "llama_index.tools.tavily_research",
"ArxivToolSpec": "llama_index.tools.arxiv",
"VectorDB": "llama_index.tools.vector_db",
Expand Down
16 changes: 13 additions & 3 deletions llama-index-core/llama_index/core/agent/react/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
ObservationReasoningStep,
)
from llama_index.core.base.llms.types import ChatMessage, MessageRole
from llama_index.core.bridge.pydantic import BaseModel, ConfigDict
from llama_index.core.bridge.pydantic import BaseModel, ConfigDict, Field
from llama_index.core.tools import BaseTool

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -53,6 +53,14 @@ class ReActChatFormatter(BaseAgentChatFormatter):

system_header: str = REACT_CHAT_SYSTEM_HEADER # default
context: str = "" # not needed w/ default
observation_role: MessageRole = Field(
default=MessageRole.USER,
description=(
"Message role of tool outputs. If the LLM you use supports function/tool "
"calling, you may set it to `MessageRole.TOOL` to avoid the tool outputs "
"being misinterpreted as new user messages."
),
)

def format(
self,
Expand All @@ -73,13 +81,13 @@ def format(
fmt_sys_header = self.system_header.format(**format_args)

# format reasoning history as alternating user and assistant messages
# where the assistant messages are thoughts and actions and the user
# where the assistant messages are thoughts and actions and the tool
# messages are observations
reasoning_history = []
for reasoning_step in current_reasoning:
if isinstance(reasoning_step, ObservationReasoningStep):
message = ChatMessage(
role=MessageRole.USER,
role=self.observation_role,
content=reasoning_step.get_content(),
)
else:
Expand All @@ -100,6 +108,7 @@ def from_defaults(
cls,
system_header: Optional[str] = None,
context: Optional[str] = None,
observation_role: MessageRole = MessageRole.USER,
) -> "ReActChatFormatter":
"""Create ReActChatFormatter from defaults."""
if not system_header:
Expand All @@ -112,6 +121,7 @@ def from_defaults(
return ReActChatFormatter(
system_header=system_header,
context=context or "",
observation_role=observation_role,
)

@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ def from_tools_or_functions(

tools = [
FunctionTool.from_defaults(fn=tool)
if not isinstance(tool, FunctionTool)
if not isinstance(tool, BaseTool)
else tool
for tool in tools_or_functions
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@
"AzureSpeechToolSpec": "llama_index.tools.azure_speech",
"WolframAlphaToolSpec": "llama_index.tools.wolfram_alpha",
"SlackToolSpec": "llama_index.tools.slack",
"LinkupToolSpec": "llama_index.tools.linkup_research",
"TavilyToolSpec": "llama_index.tools.tavily_research",
"ArxivToolSpec": "llama_index.tools.arxiv",
"VectorDB": "llama_index.tools.vector_db",
Expand Down
39 changes: 25 additions & 14 deletions llama-index-core/llama_index/core/prompts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from llama_index.core.base.llms.generic_utils import (
prompt_to_messages,
)
from llama_index.core.base.llms.types import ContentBlock, TextBlock
from llama_index.core.prompts.prompt_type import PromptType
from llama_index.core.prompts.utils import get_template_vars, format_string
from llama_index.core.types import BaseOutputParser
Expand Down Expand Up @@ -305,20 +306,30 @@ def format_messages(

messages: List[ChatMessage] = []
for message_template in self.message_templates:
message_content = message_template.content or ""

template_vars = get_template_vars(message_content)
relevant_kwargs = {
k: v for k, v in mapped_all_kwargs.items() if k in template_vars
}
content_template = message_template.content or ""

# if there's mappings specified, make sure those are used
content = format_string(content_template, **relevant_kwargs)

message: ChatMessage = message_template.model_copy()
message.content = content
messages.append(message)
# Handle messages with multiple blocks
if message_template.blocks:
formatted_blocks: List[ContentBlock] = []
for block in message_template.blocks:
if isinstance(block, TextBlock):
template_vars = get_template_vars(block.text)
relevant_kwargs = {
k: v
for k, v in mapped_all_kwargs.items()
if k in template_vars
}
formatted_text = format_string(block.text, **relevant_kwargs)
formatted_blocks.append(TextBlock(text=formatted_text))
else:
# For non-text blocks (like images), keep them as is
# TODO: can images be formatted as variables?
formatted_blocks.append(block)

message = message_template.model_copy()
message.blocks = formatted_blocks
messages.append(message)
else:
# Handle empty messages (if any)
messages.append(message_template.model_copy())

if self.output_parser is not None:
messages = self.output_parser.format_messages(messages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"open-mixtral-8x22b",
"ministral-8b-latest",
"ministral-3b-latest",
"mistral-small-latest",
"codestral-latest",
"open-mistral-nemo-latest",
)

MISTRALAI_CODE_MODELS = "codestral-latest"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exclude = ["**/BUILD"]
license = "MIT"
name = "llama-index-llms-mistralai"
readme = "README.md"
version = "0.3.1"
version = "0.3.2"

[tool.poetry.dependencies]
python = ">=3.9,<4.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
llama_index/_static
.DS_Store
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
bin/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
etc/
include/
lib/
lib64/
parts/
sdist/
share/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
.ruff_cache

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints
notebooks/

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pyvenv.cfg

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# Jetbrains
.idea
modules/
*.swp

# VsCode
.vscode

# pipenv
Pipfile
Pipfile.lock

# pyright
pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
poetry_requirements(
name="poetry",
)
Loading

0 comments on commit 5804236

Please sign in to comment.