Skip to content

Commit 94dab4b

Browse files
committed
update examples
1 parent 5904c36 commit 94dab4b

29 files changed

Lines changed: 1331 additions & 141 deletions

examples/basic/chat-local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# or you can explicitly specify it as `lm.OpenAIChatModel.GPT4` or `lm.OpenAIChatModel.GPT4_TURBO`
2323

2424
llm_config = lm.OpenAIGPTConfig(
25-
chat_model="litellm/ollama/mistral",
25+
chat_model=lm.OpenAIChatModel.GPT4_TURBO, # or,e.g. "ollama/mistral"
2626
max_output_tokens=200,
2727
chat_context_length=2048, # adjust based on your local LLM params
2828
)

examples/basic/chat-search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
-m <model_name>: to run with a different LLM model (default: gpt4-turbo)
1919
2020
You can specify a local in a few different ways, e.g. `-m local/localhost:8000/v1`
21-
or `-m litellm/ollama_chat/mistral` etc. See here how to use Langroid with local LLMs:
21+
or `-m ollama/mistral` etc. See here how to use Langroid with local LLMs:
2222
https://langroid.github.io/langroid/tutorials/local-llm-setup/
2323
2424

examples/basic/chat.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
-q <initial user msg>
1717
1818
For details on running with local or non-OpenAI models, see:
19-
https://langroid.github.io/langroid/tutorials/non-openai-llms/
19+
https://langroid.github.io/langroid/tutorials/local-llm-setup/
2020
"""
2121
import typer
2222
from rich import print
@@ -27,40 +27,12 @@
2727
from langroid.agent.chat_agent import ChatAgent, ChatAgentConfig
2828
from langroid.agent.task import Task
2929
from langroid.utils.configuration import set_global, Settings
30-
from langroid.utils.logging import setup_colored_logging
3130

3231

3332
app = typer.Typer()
3433

3534
# Create classes for non-OpenAI model configs
3635

37-
# OPTION 1: LiteLLM-supported models
38-
# -----------------------------------
39-
# For any model supported by litellm
40-
# (see list here https://docs.litellm.ai/docs/providers)
41-
# The `chat_model` should be specified as "litellm/" followed by
42-
# the chat_model name in the litellm docs.
43-
# In this case Langroid uses the litellm adapter library to
44-
# translate between OpenAI API and the model's API.
45-
# For external (remote) models, typical there will be specific env vars
46-
# (e.g. API Keys, etc) that need to be set.
47-
# If those are not set, you will get an err msg saying which vars need to be set.
48-
49-
# OPTION 2: Local models served at an OpenAI-compatible API endpoint
50-
# -----------------------------------------------------------------
51-
# Use this config for any model that is locally served at an
52-
# OpenAI-compatible API endpoint, for example, using either the
53-
# litellm proxy server https://docs.litellm.ai/docs/proxy_server
54-
# or the oooba/text-generation-webui server
55-
# https://github.com/oobabooga/text-generation-webui/tree/main/extensions/openai
56-
#
57-
# In this case the `chat_model` name should be specified as
58-
# "local/localhost:8000/v1" or "local/localhost:8000" or other port number
59-
# depending on how you launch the model locally.
60-
# Langroid takes care of extracting the local URL to set the `api_base`
61-
# of the config so that the `openai.*` completion functions can be used
62-
# without having to rely on adapter libraries like litellm.
63-
6436

6537
@app.command()
6638
def main(

examples/basic/fn-call-local-numerical.py

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Function-calling example using a local LLM, with ollama.
33
4-
"Function-calling" refers to the ability to ability of the LLM to generate
4+
"Function-calling" refers to the ability of the LLM to generate
55
a structured response, typically a JSON object, instead of a plain text response,
66
which is then interpreted by your code to perform some action.
77
This is also referred to in various scenarios as "Tools", "Actions" or "Plugins".
@@ -31,7 +31,7 @@
3131
Local model with best results is dolphin-mixtral:
3232
```
3333
ollama run dolphin-mixtral
34-
python3 examples/basic/fn-call-local-numerical.py -m litellm/ollama_chat/dolphin-mixtral:latest
34+
python3 examples/basic/fn-call-local-numerical.py -m ollama/dolphin-mixtral:latest
3535
```
3636
3737
See here for how to set up a Local LLM to work with Langroid:
@@ -43,6 +43,7 @@
4343
import fire
4444

4545
import langroid as lr
46+
from langroid.language_models.openai_gpt import OpenAICallParams
4647
from langroid.utils.configuration import settings
4748
from langroid.agent.tool_message import ToolMessage
4849
import langroid.language_models as lm
@@ -57,24 +58,17 @@
5758

5859

5960
class PolinskyTool(lr.agent.ToolMessage):
60-
"""A fictitious number transformation tool. We intentially use
61+
"""A fictitious number transformation tool. We intentionally use
6162
a fictitious tool rather than something like "square" or "double"
6263
to prevent the LLM from trying to answer the question directly.
6364
"""
6465

6566
request: str = "polinsky"
66-
purpose: str = "To respond to user request for the Polinsky transform of a <number>."
67+
purpose: str = (
68+
"To respond to user request for the Polinsky transform of a <number>."
69+
)
6770
number: int
6871

69-
def handle(self) -> str:
70-
"""Handle LLM's structured output if it matches Polinsky tool"""
71-
result = self.number * 3 + 1
72-
msg = f"""
73-
SUCCESS! The Polinksy transform of {self.number} is {result}.
74-
Present this result to me, and ask for another number.
75-
"""
76-
return msg
77-
7872
@classmethod
7973
def json_instructions(cls) -> str:
8074
inst = super().json_instructions()
@@ -84,20 +78,6 @@ def json_instructions(cls) -> str:
8478
ONLY USE THIS TOOL AFTER THE USER ASKS FOR A POLINSKY TRANSFORM.
8579
"""
8680

87-
@staticmethod
88-
def handle_message_fallback(
89-
agent: lr.ChatAgent, msg: str | ChatDocument
90-
) -> str | ChatDocument | None:
91-
"""Fallback method when LLM forgets to generate a tool"""
92-
if isinstance(msg, ChatDocument) and msg.metadata.sender == "LLM":
93-
return """
94-
You must use the "polinsky" tool/function to
95-
request the Polinsky transform of a number.
96-
You either forgot to use it, or you used it with the wrong format.
97-
Make sure all fields are filled out and pay attention to the
98-
required types of the fields.
99-
"""
100-
10181
@classmethod
10282
def examples(cls) -> List["ToolMessage"]:
10383
# Used to provide few-shot examples in the system prompt
@@ -111,6 +91,38 @@ def examples(cls) -> List["ToolMessage"]:
11191
]
11292

11393

94+
class MyChatAgent(lr.ChatAgent):
95+
tool_called: bool = False
96+
97+
def polinsky(self, msg: PolinskyTool) -> str:
98+
"""Handle LLM's structured output if it matches Polinsky tool"""
99+
self.tool_called = True
100+
result = msg.number * 3 + 1
101+
response = f"""
102+
SUCCESS! The Polinksy transform of {msg.number} is {result}.
103+
Present this result to me, and ask for another number.
104+
"""
105+
return response
106+
107+
def handle_message_fallback(
108+
self, msg: str | ChatDocument
109+
) -> str | ChatDocument | None:
110+
"""Fallback method when LLM does not generate a tool,
111+
and agent ends up handling the msg"""
112+
if isinstance(msg, ChatDocument) and msg.metadata.sender == "LLM":
113+
if self.tool_called:
114+
self.tool_called = False
115+
return "Ask the user what they need help with"
116+
else:
117+
return """
118+
You must use the "polinsky" tool/function to
119+
request the Polinsky transform of a number.
120+
You either forgot to use it, or you used it with the wrong format.
121+
Make sure all fields are filled out and pay attention to the
122+
required types of the fields.
123+
"""
124+
125+
114126
def app(
115127
m: str = DEFAULT_LLM, # model name
116128
d: bool = False, # debug
@@ -123,9 +135,13 @@ def app(
123135
chat_model=m or DEFAULT_LLM,
124136
chat_context_length=16_000, # for dolphin-mixtral
125137
max_output_tokens=100,
138+
params=OpenAICallParams(
139+
presence_penalty=0.8,
140+
frequency_penalty=0.8,
141+
),
126142
temperature=0,
127143
stream=True,
128-
timeout=45,
144+
timeout=100,
129145
)
130146

131147
# Recommended: First test if basic chat works with this llm setup as below:
@@ -143,7 +159,6 @@ def app(
143159
# verify you can interact with this in a chat loop on cmd line:
144160
# task.run("Concisely answer some questions")
145161

146-
147162
# Define a ChatAgentConfig and ChatAgent
148163
config = lr.ChatAgentConfig(
149164
llm=llm_cfg,
@@ -182,15 +197,15 @@ def app(
182197
""",
183198
)
184199

185-
agent = lr.ChatAgent(config)
200+
agent = MyChatAgent(config)
186201

187202
# (4) Enable the Tool for this agent --> this auto-inserts JSON instructions
188203
# and few-shot examples into the system message
189204
agent.enable_message(PolinskyTool)
190205

191206
# (5) Create task and run it to start an interactive loop
192207
task = lr.Task(agent)
193-
task.run("ONLY say this: 'What can I help with?' say NOTHING ELSE.")
208+
task.run("Can you help me with some questions?")
194209

195210

196211
if __name__ == "__main__":

examples/chainlit/chat-doc-qa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Note, to run this with a local LLM, you can click the settings symbol
1010
on the left of the chat window and enter the model name, e.g.:
1111
12-
litellm/ollama_chat/mistral:7b-instruct-v0.2-q8_0
12+
ollama/mistral:7b-instruct-v0.2-q8_0
1313
1414
or
1515

0 commit comments

Comments
 (0)