From ffd53e1c65a038d7675ef3316b3c68caf6cf3f2d Mon Sep 17 00:00:00 2001 From: Kye Date: Fri, 5 Apr 2024 14:05:34 -0400 Subject: [PATCH] [DOCS]Migrate from OpenAI to Swarms in 3 lines of code] --- docs/swarms_cloud/migrate_openai.md | 80 +++++++++++++++++++++++++++++ mkdocs.yml | 1 + 2 files changed, 81 insertions(+) create mode 100644 docs/swarms_cloud/migrate_openai.md diff --git a/docs/swarms_cloud/migrate_openai.md b/docs/swarms_cloud/migrate_openai.md new file mode 100644 index 00000000..81959ec3 --- /dev/null +++ b/docs/swarms_cloud/migrate_openai.md @@ -0,0 +1,80 @@ +**Quickstart** + +## Migrate from OpenAI to Swarms in 3 lines of code +If you’ve been using GPT-3.5 or GPT-4, switching to Octo AI is easy! + +Swarms VLMs are available to use through our OpenAI compatible API. Additionally, if you have been building or prototyping using OpenAI’s Python SDK you can keep your code as-is and use Swarms’s VLMs models. + +In this example, we will show you how to change just three lines of code to make your Python application use Swarms’s Open Source models through OpenAI’s Python SDK. + +​ +## Getting Started +Migrate OpenAI’s Python SDK example script to use Swarms’s LLM endpoints. + +These are the three modifications necessary to achieve our goal: + +Redefine OPENAI_API_KEY your API key environment variable to use your Swarms key. + +Redefine OPENAI_BASE_URL to point to `https://api.swarms.world/v1/chat/completions` + +Change the model name to an Open Source model, for example: cogvlm-chat-17b +​ +## Requirements +We will be using Python and OpenAI’s Python SDK. +​ +## Instructions +Set up a Python virtual environment. Read Creating Virtual Environments here. + +```sh +python3 -m venv .venv +source .venv/bin/activate +``` + +Install the pip requirements in your local python virtual environment + +`python3 -m pip install openai` +​ +## Environment setup +To run this example, there are simple steps to take: + +Get an Swarms API token by following these instructions. +Expose the token in a new SWARMS_API_TOKEN environment variable: + +`export SWARMS_API_TOKEN=` + +Switch the OpenAI token and base URL environment variable + +`export OPENAI_API_KEY=$SWARMS_API_TOKEN` +`export OPENAI_BASE_URL="https://api.swarms.world/v1/chat/completions"` + +If you prefer, you can also directly paste your token into the client initialization. + +​ +## Example code +Once you’ve completed the steps above, the code below will call Swarms LLMs: + +```python +from openai import OpenAI + +client = OpenAI() + +completion = client.chat.completions.create( + model="cogvlm-chat-17b", + messages=[ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Hello!"}, + ], +) + +print(completion.choices[0].message) +```  + +Note that you need to supply one of Swarms’s supported LLMs as an argument, as in the example above. For a complete list of our supported LLMs, check out our REST API page. + +​ +## Example output +The code above produces the following object: + +```bash +ChatCompletionMessage(content=" Hello! How can I assist you today? Do you have any questions or tasks you'd like help with? Please let me know and I'll do my best to assist you.", role='assistant' function_call=None, tool_calls=None) +``` \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index d9d0ebb8..608d306e 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -63,6 +63,7 @@ nav: - The Swarms Bounty System: "swarms_bounty_system.md" - Swarms Cloud API: - Overview: "swarms_cloud/main.md" + - Migrate from OpenAI to Swarms in 3 lines of code: "swarms_cloud/migrate_openai.md" - Swarms Framework [PY]: - Overview: "swarms/index.md" - DIY Build Your Own Agent: "diy_your_own_agent.md"