Added DeepSeek to weeks 1, 2 and 8
This commit is contained in:
@@ -23,11 +23,19 @@ class FrontierAgent(Agent):
|
||||
|
||||
def __init__(self, collection):
|
||||
"""
|
||||
Set up this instance by connecting to OpenAI, to the Chroma Datastore,
|
||||
Set up this instance by connecting to OpenAI or DeepSeek, to the Chroma Datastore,
|
||||
And setting up the vector encoding model
|
||||
"""
|
||||
self.log("Initializing Frontier Agent")
|
||||
self.openai = OpenAI()
|
||||
deepseek_api_key = os.getenv("DEEPSEEK_API_KEY")
|
||||
if deepseek_api_key:
|
||||
self.client = OpenAI(api_key=deepseek_api_key, base_url="https://api.deepseek.com")
|
||||
self.MODEL = "deepseek-chat"
|
||||
self.log("Frontier Agent is set up with DeepSeek")
|
||||
else:
|
||||
self.client = OpenAI()
|
||||
self.MODEL = "gpt-4o-mini"
|
||||
self.log("Frontier Agent is setting up with OpenAI")
|
||||
self.collection = collection
|
||||
self.model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
||||
self.log("Frontier Agent is ready")
|
||||
@@ -85,14 +93,14 @@ class FrontierAgent(Agent):
|
||||
|
||||
def price(self, description: str) -> float:
|
||||
"""
|
||||
Make a call to OpenAI to estimate the price of the described product,
|
||||
Make a call to OpenAI or DeepSeek to estimate the price of the described product,
|
||||
by looking up 5 similar products and including them in the prompt to give context
|
||||
:param description: a description of the product
|
||||
:return: an estimate of the price
|
||||
"""
|
||||
documents, prices = self.find_similars(description)
|
||||
self.log("Frontier Agent is about to call OpenAI with context including 5 similar products")
|
||||
response = self.openai.chat.completions.create(
|
||||
self.log(f"Frontier Agent is about to call {self.MODEL} with context including 5 similar products")
|
||||
response = self.client.chat.completions.create(
|
||||
model=self.MODEL,
|
||||
messages=self.messages_for(description, documents, prices),
|
||||
seed=42,
|
||||
|
||||
@@ -209,7 +209,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"test[1].prompt"
|
||||
"print(test[1].prompt)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -255,6 +255,16 @@
|
||||
" return float(match.group()) if match else 0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "06743833-c362-47f8-b02a-139be2cd52ab",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"get_price(\"The price for this is $99.99\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -306,6 +316,86 @@
|
||||
"Tester.test(gpt_4o_mini_rag, test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d793c6d0-ce3f-4680-b37d-4643f0cd1d8e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Optional Extra: Trying a DeepSeek API call instead of OpenAI\n",
|
||||
"\n",
|
||||
"If you have a DeepSeek API key, we will use it here as an alternative implementation; otherwise skip to the next section.."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "21b6a22f-0195-47b6-8f6d-cab6ebe05742",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Connect to DeepSeek using the OpenAI client python library\n",
|
||||
"\n",
|
||||
"deepseek_api_key = os.getenv(\"DEEPSEEK_API_KEY\")\n",
|
||||
"deepseek_via_openai_client = OpenAI(api_key=deepseek_api_key,base_url=\"https://api.deepseek.com\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ea7267d6-9489-4dac-a6e0-aec108e788c2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Added some retry logic here because DeepSeek is very oversubscribed and sometimes fails..\n",
|
||||
"\n",
|
||||
"def deepseek_api_rag(item):\n",
|
||||
" documents, prices = find_similars(item)\n",
|
||||
" retries = 8\n",
|
||||
" done = False\n",
|
||||
" while not done and retries > 0:\n",
|
||||
" try:\n",
|
||||
" response = deepseek_via_openai_client.chat.completions.create(\n",
|
||||
" model=\"deepseek-chat\", \n",
|
||||
" messages=messages_for(item, documents, prices),\n",
|
||||
" seed=42,\n",
|
||||
" max_tokens=8\n",
|
||||
" )\n",
|
||||
" reply = response.choices[0].message.content\n",
|
||||
" done = True\n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Error: {e}\")\n",
|
||||
" retries -= 1\n",
|
||||
" return get_price(reply)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6560faf2-4dec-41e5-95e2-b2c46cdb3ba8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"deepseek_api_rag(test[1])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0578b116-869f-429d-8382-701f1c0882f3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"Tester.test(deepseek_api_rag, test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6739870f-1eec-4547-965d-4b594e685697",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## And now to wrap this in an \"Agent\" class"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -316,6 +406,20 @@
|
||||
"from agents.frontier_agent import FrontierAgent"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2efa7ba9-c2d7-4f95-8bb5-c4295bbeb01f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Let's print the logs so we can see what's going on\n",
|
||||
"\n",
|
||||
"import logging\n",
|
||||
"root = logging.getLogger()\n",
|
||||
"root.setLevel(logging.INFO)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
|
||||
@@ -141,7 +141,9 @@
|
||||
"source": [
|
||||
"# Running the final product\n",
|
||||
"\n",
|
||||
"## Just hit shift + enter in the next cell, and let the deals flow in!!"
|
||||
"## Just hit shift + enter in the next cell, and let the deals flow in!!\n",
|
||||
"\n",
|
||||
"Note that the Frontier Agent will use DeepSeek if there's a DEEPSEEK_API_KEY in your .env file, otherwise gpt-4o-mini."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user