|
| 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 | +```` |
0 commit comments