Skip to content

Commit 7739632

Browse files
marklyszeekzhu
andauthored
[Documentation] Using non-OpenAI models (#2076)
* Addition of Non-OpenAI LLM section and main doc page * Continued writing... * Continued writing - cloud-based proxy servers * Folder renamed * Further writing * together.ai example added * Local proxy server added, diagram added, tidy up * Added vLLM to local proxy servers documentation * As per @ekzhu's feedback, individual pages and tidy up * Added reference to LM Studio and renamed file * Fixed incorrect huggingface.co link * Run pre-commit checks, added LM Studio redirect --------- Co-authored-by: Eric Zhu <[email protected]>
1 parent ecc459f commit 7739632

File tree

7 files changed

+736
-0
lines changed

7 files changed

+736
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Non-OpenAI Models
2+
3+
AutoGen allows you to use non-OpenAI models through proxy servers that provide
4+
an OpenAI-compatible API or a [custom model client](https://microsoft.github.io/autogen/blog/2024/01/26/Custom-Models)
5+
class.
6+
7+
Benefits of this flexibility include access to hundreds of models, assigning specialized
8+
models to agents (e.g., fine-tuned coding models), the ability to run AutoGen entirely
9+
within your environment, utilising both OpenAI and non-OpenAI models in one system, and cost
10+
reductions in inference.
11+
12+
## OpenAI-compatible API proxy server
13+
Any proxy server that provides an API that is compatible with [OpenAI's API](https://platform.openai.com/docs/api-reference)
14+
will work with AutoGen.
15+
16+
These proxy servers can be cloud-based or running locally within your environment.
17+
18+
![Cloud or Local Proxy Servers](images/cloudlocalproxy.png)
19+
20+
### Cloud-based proxy servers
21+
By using cloud-based proxy servers, you are able to use models without requiring the hardware
22+
and software to run them.
23+
24+
These providers can host open source/weight models, like [Hugging Face](https://huggingface.co/),
25+
or their own closed models.
26+
27+
When cloud-based proxy servers provide an OpenAI-compatible API, using them in AutoGen
28+
is straightforward. With [LLM Configuration](/docs/topics/llm_configuration) done in
29+
the same way as when using OpenAI's models, the primary difference is typically the
30+
authentication which is usually handled through an API key.
31+
32+
Examples of using cloud-based proxy servers providers that have an OpenAI-compatible API
33+
are provided below:
34+
35+
- [together.ai example](cloud-togetherai)
36+
37+
38+
### Locally run proxy servers
39+
An increasing number of LLM proxy servers are available for use locally. These can be
40+
open-source (e.g., LiteLLM, Ollama, vLLM) or closed-source (e.g., LM Studio), and are
41+
typically used for running the full-stack within your environment.
42+
43+
Similar to cloud-based proxy servers, as long as these proxy servers provide an
44+
OpenAI-compatible API, running them in AutoGen is straightforward.
45+
46+
Examples of using locally run proxy servers that have an OpenAI-compatible API are
47+
provided below:
48+
49+
- [LiteLLM with Ollama example](local-litellm-ollama)
50+
- [LM Studio](local-lm-studio)
51+
- [vLLM example](local-vllm)
52+
53+
````mdx-code-block
54+
:::tip
55+
If you are planning to use Function Calling, not all cloud-based and local proxy servers support
56+
Function Calling with their OpenAI-compatible API, so check their documentation.
57+
:::
58+
````
59+
60+
### Configuration for Non-OpenAI models
61+
62+
Whether you choose a cloud-based or locally-run proxy server, the configuration is done in
63+
the same way as using OpenAI's models, see [LLM Configuration](/docs/topics/llm_configuration)
64+
for further information.
65+
66+
You can use [model configuration filtering](/docs/topics/llm_configuration#config-list-filtering)
67+
to assign specific models to agents.
68+
69+
70+
## Custom Model Client class
71+
For more advanced users, you can create your own custom model client class, enabling
72+
you to define and load your own models.
73+
74+
See the [AutoGen with Custom Models: Empowering Users to Use Their Own Inference Mechanism](/blog/2024/01/26/Custom-Models)
75+
blog post and [this notebook](/docs/notebooks/agentchat_custom_model/) for a guide to creating custom model client classes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# Together AI
2+
This cloud-based proxy server example, using [together.ai](https://www.together.ai/), is a group chat between a Python developer
3+
and a code reviewer, who are given a coding task.
4+
5+
Start by [installing AutoGen](/docs/installation/) and getting your [together.ai API key](https://api.together.xyz/settings/profile).
6+
7+
Put your together.ai API key in an environment variable, TOGETHER_API_KEY.
8+
9+
Linux / Mac OSX:
10+
11+
```bash
12+
export TOGETHER_API_KEY=YourTogetherAIKeyHere
13+
```
14+
15+
Windows (command prompt):
16+
17+
```powershell
18+
set TOGETHER_API_KEY=YourTogetherAIKeyHere
19+
```
20+
21+
Create your LLM configuration, with the [model you want](https://docs.together.ai/docs/inference-models).
22+
23+
```python
24+
import autogen
25+
import os
26+
27+
llm_config={
28+
"config_list": [
29+
{
30+
# Available together.ai model strings:
31+
# https://docs.together.ai/docs/inference-models
32+
"model": "mistralai/Mistral-7B-Instruct-v0.1",
33+
"api_key": os.environ['TOGETHER_API_KEY'],
34+
"base_url": "https://api.together.xyz/v1"
35+
}
36+
],
37+
"cache_seed": 42
38+
}
39+
```
40+
41+
## Construct Agents
42+
43+
```python
44+
# User Proxy will execute code and finish the chat upon typing 'exit'
45+
user_proxy = autogen.UserProxyAgent(
46+
name="UserProxy",
47+
system_message="A human admin",
48+
code_execution_config={
49+
"last_n_messages": 2,
50+
"work_dir": "groupchat",
51+
"use_docker": False,
52+
}, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
53+
human_input_mode="TERMINATE",
54+
is_termination_msg=lambda x: "TERMINATE" in x.get("content"),
55+
)
56+
57+
# Python Coder agent
58+
coder = autogen.AssistantAgent(
59+
name="softwareCoder",
60+
description="Software Coder, writes Python code as required and reiterates with feedback from the Code Reviewer.",
61+
system_message="You are a senior Python developer, a specialist in writing succinct Python functions.",
62+
llm_config=llm_config,
63+
)
64+
65+
# Code Reviewer agent
66+
reviewer = autogen.AssistantAgent(
67+
name="codeReviewer",
68+
description="Code Reviewer, reviews written code for correctness, efficiency, and security. Asks the Software Coder to address issues.",
69+
system_message="You are a Code Reviewer, experienced in checking code for correctness, efficiency, and security. Review and provide feedback to the Software Coder until you are satisfied, then return the word TERMINATE",
70+
is_termination_msg=lambda x: "TERMINATE" in x.get("content"),
71+
llm_config=llm_config,
72+
)
73+
```
74+
75+
## Establish the group chat
76+
77+
```python
78+
# Establish the Group Chat and disallow a speaker being selected consecutively
79+
groupchat = autogen.GroupChat(agents=[user_proxy, coder, reviewer], messages=[], max_round=12, allow_repeat_speaker=False)
80+
81+
# Manages the group of multiple agents
82+
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
83+
```
84+
85+
## Start Chat
86+
87+
```python
88+
# Start the chat with a request to write a function
89+
user_proxy.initiate_chat(
90+
manager,
91+
message="Write a Python function for the Fibonacci sequence, the function will have one parameter for the number in the sequence, which the function will return the Fibonacci number for."
92+
)
93+
# type exit to terminate the chat
94+
```
95+
96+
Output:
97+
```` text
98+
UserProxy (to chat_manager):
99+
100+
Write a Python function for the Fibonacci sequence, the function will have one parameter for the number in the sequence, which the function will return the Fibonacci number for.
101+
102+
--------------------------------------------------------------------------------
103+
softwareCoder (to chat_manager):
104+
105+
Sure, here is a simple Python function that uses recursion to calculate the Fibonacci number:
106+
107+
```python
108+
def fibonacci(n):
109+
if n <= 0:
110+
return "Input should be a positive integer."
111+
elif n == 1:
112+
return 0
113+
elif n == 2:
114+
return 1
115+
else:
116+
return fibonacci(n-1) + fibonacci(n-2)
117+
```
118+
119+
This function takes an integer `n` as input and returns the `n`th number in the Fibonacci sequence. The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.
120+
121+
Note that this implementation uses recursion and may not be efficient for large values of `n`. In such cases, an iterative approach or memoization would be more appropriate.
122+
123+
--------------------------------------------------------------------------------
124+
codeReviewer (to chat_manager):
125+
126+
I see a couple of issues with the current implementation of the `fibonacci` function:
127+
128+
1. The function does not handle negative inputs correctly. Currently, it returns a string message for any input less than or equal to 0. It would be better to raise a `ValueError` or return a more informative message.
129+
2. The function uses recursion to calculate the Fibonacci number, which can be inefficient for large inputs. A more efficient approach would be to use an iterative approach or memoization.
130+
131+
Here's an updated implementation that addresses these issues:
132+
133+
```python
134+
def fibonacci(n):
135+
if n <= 0:
136+
raise ValueError("Input should be a positive integer.")
137+
elif n == 1:
138+
return 0
139+
elif n == 2:
140+
return 1
141+
else:
142+
a, b = 0, 1
143+
for _ in range(n - 2):
144+
a, b = b, a + b
145+
return b
146+
```
147+
148+
This implementation uses a loop to calculate the Fibonacci number iteratively, which is more efficient than the recursive approach. It also raises a `ValueError` for negative inputs, which is a more appropriate way to handle invalid inputs.
149+
150+
--------------------------------------------------------------------------------
151+
152+
>>>>>>>> USING AUTO REPLY...
153+
154+
>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
155+
UserProxy (to chat_manager):
156+
157+
exitcode: 0 (execution succeeded)
158+
Code output:
159+
160+
161+
--------------------------------------------------------------------------------
162+
codeReviewer (to chat_manager):
163+
164+
I'm glad the updated implementation addresses the issues with the original code. Let me know if you have any further questions or if there's anything else I can help you with.
165+
166+
To terminate the conversation, please type "TERMINATE".
167+
168+
--------------------------------------------------------------------------------
169+
Please give feedback to chat_manager. Press enter or type 'exit' to stop the conversation: exit
170+
````
Loading

0 commit comments

Comments
 (0)