-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# Train an Expert Agent in Sophia III | ||
# This example assumes that you have your Sophia III server set up and running and that you have an OpenAI API Key to use for setting up this agent. | ||
# You can use any other provider, but this example specifically uses OpenAI for the agent. | ||
|
||
# Create the Agent | ||
# For this example, we will create an expert on Sophia III. We will use the OpenAI Provider and gpt-4o model in this example. | ||
# These settings can be easily changed in the streamlit app or over API. | ||
|
||
from sophiaiiisdk import SophiaIIISDK | ||
|
||
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 = SophiaIIISDK(base_uri=sophia_server, api_key=api_key) | ||
|
||
# If you don't have your JWT but have your authenticator: | ||
# sophia = SophiaIIISDK(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": "default", | ||
"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, | ||
"NORAMA01_API_KEY": "", | ||
"NORAMA01_API_URI": "", | ||
"NORAMA01_VOICE": "DukeNukem", | ||
"NORAMA01_TEMPERATURE": 1.33, | ||
"NORAMA01_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", | ||
"NORAMA01_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 | ||
# 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. | ||
|
||
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 | ||
# 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. | ||
|
||
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. | ||
# See this link for more information to take advantage of the abilities of this endpoint: https://nora-ma01.github.io/SophiaIII/2-Concepts/04-Chat%20Completions.html | ||
|
||
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 is 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. |