Skip to content

Commit

Permalink
Update llm
Browse files Browse the repository at this point in the history
  • Loading branch information
dewmal committed Jul 2, 2024
1 parent 7643678 commit f0591fc
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
Empty file.
63 changes: 63 additions & 0 deletions bindings/ceylon/ceylon/tools/file_publisher_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import unittest
from typing import Any, Type, Optional
from unittest.mock import patch, mock_open, Mock

from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.tools import BaseTool


class FilePublisherSchema(BaseModel):
"""Input for SendMessageTool."""

file_content: str = Field(
...,
description="The file content to be published. This should be a string containing the text or data that needs to be published.",
)
file_name: str = Field(
...,
description="The file name to be created. This should be a string containing the name of the file to be created.",
)


class FilePublisherTool(BaseTool):
name = "File Publisher"
description: str = (
"""
Use this tool to publish content in a file. use the `file_content` and `file_name` parameters
to define the content and the name of the file to be created.
parameters:
file_content (str): The content to be published. This should be a string containing the text or data that needs to be published.
file_name (str): The name of the file to be created. This should be a string containing the name of the file to be created.
"""
)

args_schema: Type[FilePublisherSchema] = FilePublisherSchema

def _run(self,
file_content: str,
file_name: str,
run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str:
"""
Publishes the given content.
Parameters:
file_content (str): The content to be published. This should be a string containing the text or data that needs to be published.
file_name (str): The name of the file to be created. This should be a string containing the name of the file to be created.
Returns:
None
"""
print(f"Publishing content")
name = f"content-{file_name}.txt"

try:
# Open the file in write mode
with open(name, "a", encoding="utf-8") as f:
f.write(file_content)
return f"Published {file_content} in {name}"
except Exception as e:
return f"An error occurred: {e}"

async def _arun(self, *args: Any, **kwargs: Any, ) -> Any:
return self._run(*args, **kwargs)
44 changes: 44 additions & 0 deletions bindings/ceylon/ceylon/tools/search_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import Any, Type, Optional

from duckduckgo_search import DDGS
from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_core.tools import BaseTool
from pydantic.v1 import BaseModel, Field


class SearchToolInputSchema(BaseModel):
"""Input for SearchTool."""

query: str = Field(
...,
description="The query to search for. This should be a string containing the search terms.",
)


class SearchTool(BaseTool):
name = "Search"
description: str = (
"Use this tool to search on DuckDuckGo. Use the `query` parameter to define the search terms. its a string containing the search terms. "
)

args_schema: Type[SearchToolInputSchema] = SearchToolInputSchema

def _run(self, query: str,
run_manager: Optional[CallbackManagerForToolRun] = None, ):
"""
Searches the given keywords on DuckDuckGo and returns the search results.
Parameters:
query (str): The keywords to search for. This should be a string containing the search terms.
Returns:
list: A list of dictionaries, where each dictionary contains the following keys:
- title (str): The title of the search result.
- href (str): The URL of the search result.
- body (str): A brief description of the search result.
"""
print(f"Searching for {query}")
results = DDGS().text(query, safesearch='off', timelimit='y', max_results=10)
return results

async def _arun(self, *args: Any, **kwargs: Any, ) -> Any:
return self._run(*args, **kwargs)
Empty file.
55 changes: 55 additions & 0 deletions bindings/ceylon/tests/tools/file_publisher_tool_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import unittest
from unittest.mock import Mock, patch

from langchain_community.llms.ollama import Ollama

from ceylon.ceylon import AgentDefinition
from ceylon.llm.llm_caller import process_agent_request
from ceylon.tools.search_tool import SearchTool


class TestProcessAgentRequest(unittest.TestCase):

def setUp(self):
self.llm = Ollama(model="phi3:instruct")
self.search_tool = SearchTool()
self.tools = [self.search_tool]

@patch('ceylon.llm.llm_caller.process_agent_request')
def test_process_agent_request(self, mock_process_agent_request):
# Arrange
expected_result = "Mocked result of process_agent_request"
mock_process_agent_request.return_value = expected_result

inputs = {"task_info": "How LLM Work"}
agent_definition = AgentDefinition(
name="Agent 1",
position="Content Publisher",
responsibilities=[
"Write content based on the given topic.",
"Write content to file",
],
instructions=[
"Write given content in a clear and concise manner.",
],
id="Agent 1"
)

# Act
result = process_agent_request(
self.llm,
inputs=inputs,
agent_definition=agent_definition,
tools=self.tools
)

print(result)

def test_ollama_initialization(self):
# Arrange
expected_model = "phi3:instruct"

llm = Ollama(model=expected_model)

# Assert
self.assertEqual(llm.model, expected_model)

0 comments on commit f0591fc

Please sign in to comment.