Skip to content

Commit

Permalink
Improve examples
Browse files Browse the repository at this point in the history
  • Loading branch information
NoraMA-01 authored Jan 21, 2025
1 parent 81d33ad commit f2c5da5
Showing 1 changed file with 177 additions and 135 deletions.
312 changes: 177 additions & 135 deletions examples/SOPHIA-Expert-ezLocalai.ipynb
Original file line number Diff line number Diff line change
@@ -1,135 +1,177 @@
Train an Expert Agent in Sophia III with ezLocalai Provider
This example assumes that you have your Sophia III and ezLocalai servers set up and running. This example is specifically for running Sophia III with ezLocalai and training a local agent. If you do not wish to run local models, we have the same example set up for using an agent with the OpenAI API.

If you do not have ezLocalai set up and you want to set it up to run local models, go to https://github.com/DevXT-LLC/ezlocalai and follow the instructions there to set up the server, then continue with this example.

Create the Agent
For this example, we will create an expert on Sophia III. These settings can be easily changed in the Streamlit app or over API.

Modify the sophia_server, api_key, agent_name, EZLOCALAI_API_URL, EZLOCALAI_API_KEY, persona, and any others as needed.

python
Kopyala
Düzenle
from agixtsdk import AGiXTSDK

sophia_server = "http://localhost:7437" # Change this to your Sophia III server URL
api_key = "None" # Change this to your Sophia III API key (This should be your JWT in the web interface)
sophia = AGiXTSDK(base_uri=sophia_server, api_key=api_key)

# If you don't have your JWT but have your authenticator:
# sophia = AGiXTSDK(base_uri=sophia_server)
# sophia.login(email="Your email address", otp="123456")

agent_name = "Sophia III" # Change this if desired

sophia.add_agent(
agent_name=agent_name,
settings={
"provider": "rotation",
"vision_provider": "rotation",
"tts_provider": "ezlocalai",
"transcription_provider": "default",
"translation_provider": "default",
"embeddings_provider": "default",
"image_provider": "None",
"ANTHROPIC_API_KEY": "",
"ANTHROPIC_MODEL": "claude-3-5-sonnet-20241022",
"AZURE_MODEL": "",
"AZURE_API_KEY": "",
"AZURE_OPENAI_ENDPOINT": "",
"AZURE_DEPLOYMENT_NAME": "",
"AZURE_TEMPERATURE": 0.7,
"AZURE_TOP_P": 0.95,
"DEEPSEEK_API_KEY": "",
"DEEPSEEK_MODEL": "deepseek-chat",
"GOOGLE_API_KEY": "",
"GOOGLE_MODEL": "gemini-exp-1206",
"GOOGLE_TEMPERATURE": 0.7,
"GOOGLE_TOP_P": 0.95,
"EZLOCALAI_API_URI": "http://ezlocalai:8091/v1/", # URL for the EZLOCALAI API, change this to your EZLOCALAI API URL. Never use localhost here, it is a different container.
"EZLOCALAI_API_KEY": "Your EZLOCALAI API key", # Change this to your EZLOCALAI API key
"EZLOCALAI_VOICE": "DukeNukem", # Voice for TTS, change this to the voice you want to use from ezlocalai.
"EZLOCALAI_TEMPERATURE": 1.33,
"EZLOCALAI_TOP_P": 0.95,
"OPENAI_API_KEY": "", # Enter your OpenAI API key here
"OPENAI_MODEL": "chatgpt-4o-latest",
"XAI_API_KEY": "",
"XAI_MODEL": "grok-beta",
"EZLOCALAI_MAX_TOKENS": "1",
"DEEPSEEK_MAX_TOKENS": "60000",
"AZURE_MAX_TOKENS": "100000",
"XAI_MAX_TOKENS": "120000",
"OPENAI_MAX_TOKENS": "128000",
"ANTHROPIC_MAX_TOKENS": "200000",
"GOOGLE_MAX_TOKENS": "2097152",
"SMARTEST_PROVIDER": "anthropic",
"mode": "prompt",
"prompt_name": "Think About It",
"prompt_category": "Default",
"analyze_user_input": False,
"websearch": False,
"websearch_depth": 2,
"WEBSEARCH_TIMEOUT": 0,
"persona": "Sophia III is an expert on the Sophia III AI agent automation platform and supports the users of Sophia III.", # Use this field to set persona for the AI model
"tts": False,
},
training_urls=[], # Add training URLs here if you want to train the agent
)
Zip Your Training Data
Create a zip file called training_data.zip of the Sophia III docs folder. You can change this to any folder that you would like to use as training data, or skip this step and use an existing zip file.

A good example of what to use for training data would be any PDF, Word document, text file, or any other kind of file with information in it that you would like the agent to learn from.

python
Kopyala
Düzenle
from zipfile import ZipFile
import os

os.chdir("../")
with ZipFile("examples/training_data.zip", "w") as zipObj:
for foldername, subfolders, filenames in os.walk("docs"):
for filename in filenames:
file_path = os.path.join(foldername, filename)
zipObj.write(file_path)
os.chdir("examples/")
Train the Agent on the Training Data
Train the agent on the training data you provided. This process duration depends on the training data size.

python
Kopyala
Düzenle
import base64

zip_file_name = "training_data.zip"
training_data = base64.b64encode(open(zip_file_name, "rb").read()).decode("utf-8")

sophia.learn_file(
agent_name=agent_name,
file_name=zip_file_name,
file_content=training_data,
collection_number="0",
)
Chat with Your Trained Expert Sophia III Agent
Sophia III has direct support for using the OpenAI API for chat completions.

python
Kopyala
Düzenle
import openai

prompt = "What can you tell me about Sophia III?"

openai.base_url = f"{sophia_server}/v1/"
openai.api_key = api_key

response = openai.chat.completions.create(
model=agent_name,
messages=[{"role": "user", "content": prompt}],
user="Tell me about Sophia III", # This field is used for the conversation name; if empty, it will use today's date
)
print(response.choices[0].message.content)
That's All!
You now have a trained expert agent in Sophia III. This agent will be able to support users by answering questions, providing information, and more about Sophia III.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Train an Expert Agent in Sophia III with ezLocalai Provider\n",
"\n",
"This example assumes that you have your Sophia III and ezLocalai servers set up and running. This example is specifically for running Sophia III with ezLocalai and training a local agent. If you do not wish to run local models, we have the same example set up for using an agent with the OpenAI API.\n",
"\n",
"If you do not have ezLocalai set up and you want to set it up to run local models, go to https://github.com/DevXT-LLC/ezlocalai and follow the instructions there to set up the server, then continue with this example.\n",
"\n",
"## Create the Agent\n",
"\n",
"For this example, we will create an expert on Sophia III. These settings can be easily changed in the streamlit app or over API.\n",
"\n",
"Modify the `agixt_server`, `api_key`, `agent_name`, `EZLOCALAI_API_URL`, `EZLOCALAI_API_KEY`, `persona`, and any others as needed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from agixtsdk import AGiXTSDK\n",
"\n",
"agixt_server = \"http://localhost:7437\" # Change this to your Sophia III server URL\n",
"\n",
"api_key = \"None\" # Change this to your Sophia III API key (This should be your JWT in the web interface)\n",
"agixt = AGiXTSDK(base_uri=agixt_server, api_key=api_key)\n",
"\n",
"# If you dont have your jwt but have your authenticator:\n",
"# agixt = AGiXTSDK(base_uri=agixt_server)\n",
"# agixt.login(email=\"Your email address\", otp=\"123456\")\n",
"\n",
"\n",
"agent_name = \"Sophia III\" # Change this if desired\n",
"\n",
"\n",
"agixt.add_agent(\n",
" agent_name=agent_name,\n",
" settings={\n",
" \"provider\": \"rotation\",\n",
" \"vision_provider\": \"rotation\",\n",
" \"tts_provider\": \"ezlocalai\",\n",
" \"transcription_provider\": \"default\",\n",
" \"translation_provider\": \"default\",\n",
" \"embeddings_provider\": \"default\",\n",
" \"image_provider\": \"None\",\n",
" \"ANTHROPIC_API_KEY\": \"\",\n",
" \"ANTHROPIC_MODEL\": \"claude-3-5-sonnet-20241022\",\n",
" \"AZURE_MODEL\": \"\",\n",
" \"AZURE_API_KEY\": \"\",\n",
" \"AZURE_OPENAI_ENDPOINT\": \"\",\n",
" \"AZURE_DEPLOYMENT_NAME\": \"\",\n",
" \"AZURE_TEMPERATURE\": 0.7,\n",
" \"AZURE_TOP_P\": 0.95,\n",
" \"DEEPSEEK_API_KEY\": \"\",\n",
" \"DEEPSEEK_MODEL\": \"deepseek-chat\",\n",
" \"GOOGLE_API_KEY\": \"\",\n",
" \"GOOGLE_MODEL\": \"gemini-exp-1206\",\n",
" \"GOOGLE_TEMPERATURE\": 0.7,\n",
" \"GOOGLE_TOP_P\": 0.95,\n",
" \"EZLOCALAI_API_URI\": \"http://ezlocalai:8091/v1/\", # URL for the EZLOCALAI API, change this to your EZLOCALAI API URL. Never use localhost here, it is a different container.\n",
" \"EZLOCALAI_API_KEY\": \"Your EZLOCALAI API key\", # Change this to your EZLOCALAI API key\n",
" \"EZLOCALAI_VOICE\": \"DukeNukem\", # Voice for TTS, change this to the voice you want to use from ezlocalai.\n",
" \"EZLOCALAI_TEMPERATURE\": 1.33,\n",
" \"EZLOCALAI_TOP_P\": 0.95,\n",
" \"OPENAI_API_KEY\": \"\", # Enter your OpenAI API key here\n",
" \"OPENAI_MODEL\": \"chatgpt-4o-latest\",\n",
" \"XAI_API_KEY\": \"\",\n",
" \"XAI_MODEL\": \"grok-beta\",\n",
" \"EZLOCALAI_MAX_TOKENS\": \"1\",\n",
" \"DEEPSEEK_MAX_TOKENS\": \"60000\",\n",
" \"AZURE_MAX_TOKENS\": \"100000\",\n",
" \"XAI_MAX_TOKENS\": \"120000\",\n",
" \"OPENAI_MAX_TOKENS\": \"128000\",\n",
" \"ANTHROPIC_MAX_TOKENS\": \"200000\",\n",
" \"GOOGLE_MAX_TOKENS\": \"2097152\",\n",
" \"SMARTEST_PROVIDER\": \"anthropic\",\n",
" \"mode\": \"prompt\",\n",
" \"prompt_name\": \"Think About It\",\n",
" \"prompt_category\": \"Default\",\n",
" \"analyze_user_input\": False,\n",
" \"websearch\": False,\n",
" \"websearch_depth\": 2,\n",
" \"WEBSEARCH_TIMEOUT\": 0,\n",
" \"persona\": \"Sophia III is an expert on the Sophia III AI agent automation platform and supports the users of Sophia III.\", # Use this field to set persona for the AI model\n",
" \"tts\": False,\n",
" },\n",
" training_urls=[], # Add training URLs here if you want to train the agent\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Zip your training data\n",
"\n",
"Creates a zip file called `training_data.zip` of the Sophia III `docs` folder. You can change this to any folder that you would like to use as training data, or skip this step and use an existing zip file.\n",
"\n",
"A good example of what to use for training data would be any PDF, word document, text file, or any other kind of file with information in it that you would like the agent to learn from."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from zipfile import ZipFile\n",
"import os\n",
"\n",
"os.chdir(\"../\")\n",
"with ZipFile(\"examples/training_data.zip\", \"w\") as zipObj:\n",
" for foldername, subfolders, filenames in os.walk(\"docs\"):\n",
" for filename in filenames:\n",
" file_path = os.path.join(foldername, filename)\n",
" zipObj.write(file_path)\n",
"os.chdir(\"examples/\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Train the Agent on the training data\n",
"\n",
"This will train the agent on the training data that you have provided. This will take some time to complete depending on the size of the training data. A zip file around 70MB in size takes around 3 minutes to complete. The Sophia III docs should complete very quickly since it is all markdown files totaling around 3MB."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import base64\n",
"\n",
"zip_file_name = \"training_data.zip\"\n",
"training_data = base64.b64encode(open(zip_file_name, \"rb\").read()).decode(\"utf-8\")\n",
"\n",
"agixt.learn_file(\n",
" agent_name=agent_name,\n",
" file_name=zip_file_name,\n",
" file_content=training_data,\n",
" collection_number=\"0\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Chat with your trained expert Sophia III agent\n",
"\n",
"Sophia III has direct support for using the OpenAI API for chat completions. See this link for more information to take advantage of the abilities of this endpoint: https://nora-ma-01.github.io/Sophia-III/2-Concepts/04-Chat%20Completions.html\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"\n",
"prompt = \"What can you tell me about Sophia III?\"\n",
"\n",
"openai.base_url = f\"{agixt_server}/v1/\"\n",
"openai.api_key = api_key\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=agent_name,\n",
" messages=[{\"role\": \"

0 comments on commit f2c5da5

Please sign in to comment.