Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pchalasani committed Sep 23, 2023
1 parent 1e6f283 commit e2b7477
Show file tree
Hide file tree
Showing 8 changed files with 436 additions and 14 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ python3 -m venv .venv
# activate the virtual env
. .venv/bin/activate

pip install langroid
# install with `hf-embeddings` extra to be able to use sentence_transformers embeddings
pip install "langroid[hf-embeddings]"

# or to update an existing installation:
pip install --upgrade "langroid[hf-embeddings]"
```


Expand Down
109 changes: 98 additions & 11 deletions examples/basic/chat-search.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
"""
This is a basic example of a chatbot that uses the GoogleSearchTool:
when the LLM doesn't know the answer to a question, it will use the tool to
search the web for relevant results, and then use the results to answer the
question.
NOTE: running this example requires setting the GOOGLE_API_KEY and GOOGLE_CSE_ID
environment variables in your `.env` file, as explained in the
[README](https://github.com/langroid/langroid#gear-installation-and-setup).
"""

import typer
from rich import print
from rich.prompt import Prompt
from pydantic import BaseSettings
from dotenv import load_dotenv

from langroid.agent.chat_agent import ChatAgent, ChatAgentConfig
from langroid.agent.task import Task
from langroid.agent.stateless_tools.google_search_tool import GoogleSearchTool
from langroid.language_models.openai_gpt import OpenAIChatModel, OpenAIGPTConfig
from langroid.language_models.base import LocalModelConfig
from langroid.agent.tools.google_search_tool import GoogleSearchTool
from langroid.language_models.openai_gpt import OpenAIGPTConfig
from langroid.utils.configuration import set_global, Settings
from langroid.utils.logging import setup_colored_logging

Expand All @@ -15,10 +29,27 @@
setup_colored_logging()


def chat() -> None:
class CLIOptions(BaseSettings):
local: bool = False
api_base: str = "http://localhost:8000/v1"
local_model: str = ""
local_ctx: int = 2048
# use completion endpoint for chat?
# if so, we should format chat->prompt ourselves, if we know the required syntax
completion: bool = False

class Config:
extra = "forbid"
env_prefix = ""


def chat(opts: CLIOptions) -> None:
print(
"""
[blue]Welcome to the basic chatbot!
[blue]Welcome to the Google Search chatbot!
I will try to answer your questions, relying on (summaries of links from)
Google Search when needed.
Enter x or q to quit at any point.
"""
)
Expand All @@ -27,24 +58,60 @@ def chat() -> None:
default="Default: 'You are a helpful assistant'",
)

load_dotenv()

# create the appropriate OpenAIGPTConfig depending on local model or not

if opts.local or opts.local_model:
# assumes local endpoint is either the default http://localhost:8000/v1
# or if not, it has been set in the .env file as the value of
# OPENAI_LOCAL.API_BASE
local_model_config = LocalModelConfig(
api_base=opts.api_base,
model=opts.local_model,
context_length=opts.local_ctx,
use_completion_for_chat=opts.completion,
)
llm_config = OpenAIGPTConfig(
local=local_model_config,
timeout=180,
)
else:
# defaults to chat_model = OpenAIChatModel.GPT4
llm_config = OpenAIGPTConfig()

config = ChatAgentConfig(
system_message=sys_msg,
llm=OpenAIGPTConfig(
chat_model=OpenAIChatModel.GPT4,
),
llm=llm_config,
vecdb=None,
)
agent = ChatAgent(config)
agent.enable_message(GoogleSearchTool)
task = Task(
agent,
system_message="""
You are a helpful assistant. You will try your best to answer my questions.
If you don't know you can use up to 2 results from the `web search`
tool/function-call to help you with answering the question.
If you cannot answer from your own knowledge, you can use up to 5
results from the `web_search` tool/function-call to help you with
answering the question.
Be very concise in your responses, use no more than 1-2 sentences.
When you answer based on a web search, First show me your answer,
and then show me the SOURCE(s) and EXTRACT(s) to justify your answer,
in this format:
<your answer here>
SOURCE: https://www.wikihow.com/Be-a-Good-Assistant-Manager
EXTRACT: Be a Good Assistant ... requires good leadership skills.
SOURCE: ...
EXTRACT: ...
For the EXTRACT, ONLY show up to first 3 words, and last 3 words.
""",
)
task.run()
# local models do not like the first message to be empty
user_message = "Hello." if (opts.local or opts.local_model) else None
task.run(user_message)


@app.command()
Expand All @@ -55,6 +122,19 @@ def main(
cache_type: str = typer.Option(
"redis", "--cachetype", "-ct", help="redis or momento"
),
local: bool = typer.Option(False, "--local", "-l", help="use local llm"),
local_model: str = typer.Option(
"", "--local_model", "-lm", help="local model path"
),
api_base: str = typer.Option(
"http://localhost:8000/v1", "--api_base", "-api", help="local model api base"
),
local_ctx: int = typer.Option(
2048, "--local_ctx", "-lc", help="local llm context size (default 2048)"
),
completion: bool = typer.Option(
False, "--completion", "-c", help="use completion endpoint for chat"
),
) -> None:
set_global(
Settings(
Expand All @@ -64,7 +144,14 @@ def main(
cache_type=cache_type,
)
)
chat()
opts = CLIOptions(
local=local,
api_base=api_base,
local_model=local_model,
local_ctx=local_ctx,
completion=completion,
)
chat(opts)


if __name__ == "__main__":
Expand Down
Binary file added examples/data-qa/sql-chat/demo.db
Binary file not shown.
25 changes: 25 additions & 0 deletions examples/data-qa/sql-chat/demo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"departments": {
"description": "The 'departments' table holds details about the various departments. It relates to the 'employees' table via a foreign key in the 'employees' table.",
"columns": {
"id": "A unique identifier for a department. This ID is used as a foreign key in the 'employees' table.",
"name": "The name of the department."
}
},
"employees": {
"description": "The 'employees' table contains information about the employees. It relates to the 'departments' and 'sales' tables via foreign keys.",
"columns": {
"id": "A unique identifier for an employee. This ID is used as a foreign key in the 'sales' table.",
"name": "The name of the employee.",
"department_id": "The ID of the department the employee belongs to. This is a foreign key referencing the 'id' in the 'departments' table."
}
},
"sales": {
"description": "The 'sales' table keeps a record of all sales made by employees. It relates to the 'employees' table via a foreign key.",
"columns": {
"id": "A unique identifier for a sale.",
"amount": "The amount of the sale in eastern Caribbean dollars (XCD).",
"employee_id": "The ID of the employee who made the sale. This is a foreign key referencing the 'id' in the 'employees' table."
}
}
}
Loading

0 comments on commit e2b7477

Please sign in to comment.