Skip to content

Commit

Permalink
update examples, fix bug in OpenAIEmbeddingModel, update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
krohling committed Jan 14, 2024
1 parent ac9bd20 commit 2481642
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 55 deletions.
2 changes: 1 addition & 1 deletion bondai/models/openai/openai_embedding_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def max_tokens(self) -> int:

def create_embedding(self, prompt: str) -> List[float] | List[List[float]]:
return create_embedding(
prompt, self._model, connection_params=self._connection_params
prompt, connection_params=self._connection_params, model=self._model
)

def count_tokens(self, prompt: str) -> int:
Expand Down
4 changes: 2 additions & 2 deletions bondai/models/openai/openai_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def calculate_cost(model_name: str, usage: Dict):
else:
embedding_tokens += token_count
embedding_costs += token_count * model["price_per_token"]
else:
print(f"Unknown model: {model_name}")
# else:
# print(f"Unknown model: {model_name}")


def get_max_tokens(model: str) -> int:
Expand Down
82 changes: 54 additions & 28 deletions tests/memory/single_agent_with_memory.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,61 @@
from datetime import datetime
from bondai.models.openai import get_total_cost
from bondai.tools.file import FileWriteTool
from bondai.agents import ConversationalAgent
from bondai.memory import MemoryManager
from util import extract_text_from_directory

memory_manager = MemoryManager()
from bondai.agents import ConversationalAgent, AgentEventNames
from bondai.models.openai import OpenAIEmbeddingModel
from bondai.memory import (
MemoryManager,
InMemoryCoreMemoryDataSource,
InMemoryArchivalMemoryDataSource,
)

import io
import requests
from PyPDF2 import PdfReader
from bondai.util import split_text


def retrieve_and_parse_pdf(url):
response = requests.get(url)
if response.status_code == 200:
pdf = PdfReader(io.BytesIO(response.content))
text = ""
for page in pdf.pages:
text += page.extract_text() + "\n"

return split_text(OpenAIEmbeddingModel(), text)
else:
return f"Error retrieving PDF: {response.status_code}"


memory_manager = MemoryManager(
core_memory_datasource=InMemoryCoreMemoryDataSource(),
archival_memory_datasource=InMemoryArchivalMemoryDataSource(),
)

memory_manager.core_memory.set(
"user", "Name is George. Lives in New York. Has a dog named Max."
)
memory_manager.archival_memory.insert_bulk(
extract_text_from_directory("./tests/memory/documents")
retrieve_and_parse_pdf("https://arxiv.org/pdf/2310.10501.pdf")
)

agent = ConversationalAgent(tools=[FileWriteTool()], memory_manager=memory_manager)

message = "Start the conversation by sending the first message. You can exit any time by typing 'exit'."
while True:
user_input = input(message + "\n")
if user_input.lower() == "exit":
break
response = agent.send_message(user_input)
response.success = True
response.completed_at = datetime.now()

if response:
message = response.message
if message.lower() == "exit":
break
else:
print("The agent has exited the conversation.")
break
agent = ConversationalAgent(memory_manager=memory_manager)
agent.on(
AgentEventNames.TOOL_COMPLETED,
lambda _, m: print(
f"*************\nTool: {m.tool_name}({str(m.tool_arguments)})\nOutput: {m.tool_output}\n\n"
),
)

print(f"Total Cost: {get_total_cost()}")
response = agent.send_message("Do you know my name?")
print(response.message)

response = agent.send_message("Actually my name is Kevin.")
print(response.message)

response = agent.send_message(
(
"Can you check your archival memory to see what information you have about Nemo Guardrails? "
"I'd like a full summary of the information you have about the project including an example "
"that demonstrates how to use Colang."
)
)
print(response.message)
29 changes: 6 additions & 23 deletions tests/vision/single_agent_with_vision.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
from datetime import datetime
from bondai.models.openai import get_total_cost
from bondai.tools.vision import ImageAnalysisTool
from bondai.agents import ConversationalAgent
from bondai.agents import Agent

agent = ConversationalAgent(tools=[ImageAnalysisTool()])

message = "Start the conversation by sending the first message. You can exit any time by typing 'exit'."
while True:
user_input = input(message + "\n")
if user_input.lower() == "exit":
break
response = agent.send_message(user_input)
response.success = True
response.completed_at = datetime.now()

if response:
message = response.message
if message.lower() == "exit":
break
else:
print("The agent has exited the conversation.")
break

print(f"Total Cost: {get_total_cost()}")
agent = Agent(tools=[ImageAnalysisTool()])
result = agent.run(
"What kind of animal this is? https://www.forbes.com/advisor/wp-content/uploads/2023/09/getty_creative.jpeg-900x510.jpg"
)
result.tool_arguments["results"]
2 changes: 1 addition & 1 deletion website/docs/examples/online-research/online-research.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Next we will create an Agent with 3 tools:
- **FileWriteTool** Allows the Agent to save the results to a file.

```python
from bondai import Agent
from bondai.agents import Agent
from bondai.tools.search import GoogleSearchTool
from bondai.tools.website import WebsiteQueryTool
from bondai.tools.file import FileWriteTool
Expand Down

0 comments on commit 2481642

Please sign in to comment.