Skip to content

Commit

Permalink
Updates:
Browse files Browse the repository at this point in the history
- Significant imporvement on tool-detection mechanis. Add the ability of re-evaluation.
- Add a few shot examples.
- Refactor tools handler class.
- Update assistant example in cookbook.
  • Loading branch information
unclecode committed Mar 20, 2024
1 parent 9b13523 commit d5ee25c
Show file tree
Hide file tree
Showing 7 changed files with 5,882 additions and 150 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# GroqCall.ai - Lightning-Fast LLM Function Calls

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1q3is7qynCsx4s7FBznCfTMnokbKWIv1F?usp=sharing)
[![Version](https://img.shields.io/badge/version-0.0.2-blue.svg)](https://github.com/unclecode/groqcall)
[![Version](https://img.shields.io/badge/version-0.0.4-blue.svg)](https://github.com/unclecode/groqcall)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

GroqCall is a proxy server that enables lightning-fast function calls for Groq's Language Processing Unit (LPU) and other AI providers. It simplifies the creation of AI assistants by offering a wide range of built-in functions hosted on the cloud.
Expand Down
338 changes: 223 additions & 115 deletions app/libs/tools_handler.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ async def index(request: Request):
# Add an get endpoint simple return the evrsion of the app
@app.get("/version")
async def version():
return {"version": "0.0.3"}
return {"version": "0.0.4"}


if __name__ == "__main__":
import uvicorn

# uvicorn.run("main:app", host=os.getenv("HOST"), port=int(os.getenv('PORT')), workers=1, reload=True)
uvicorn.run(
"main:app", host=os.getenv("HOST"), port=int(os.getenv("PORT")), workers=1
"main:app", host=os.getenv("HOST"), port=int(os.getenv("PORT")), workers=1, reload=False
)
335 changes: 319 additions & 16 deletions app/prompts.py

Large diffs are not rendered by default.

18 changes: 7 additions & 11 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ def create_logger(logger_name: str, log_path: str = ".logs/access.log", show_on_
return logger


def get_tool_call_response(completion, unresolved_tol_calls, resolved_responses):
def get_tool_call_response(tool_calls_result, unresolved_tol_calls, resolved_responses):
last_completion = tool_calls_result["last_completion"]
tool_response = {
"id": "chatcmpl-" + completion.id,
"id": "chatcmpl-" + last_completion.id if last_completion else None,
"object": "chat.completion",
"created": completion.created,
"model": completion.model,
"created": last_completion.created if last_completion else None,
"model": last_completion.model if last_completion else None,
"choices": [
{
"index": 0,
Expand All @@ -49,14 +50,9 @@ def get_tool_call_response(completion, unresolved_tol_calls, resolved_responses)
}
],
"resolved": resolved_responses,
"usage": {
"prompt_tokens": completion.usage.prompt_tokens,
"completion_tokens": completion.usage.completion_tokens,
"total_tokens": completion.usage.total_tokens,
},
"system_fingerprint": completion.system_fingerprint,
"usage": tool_calls_result["usage"],
"system_fingerprint": last_completion.system_fingerprint if last_completion else None,
}

return tool_response

def describe(prompt: str, image_url_or_base64 : str, **kwargs) -> str:
Expand Down
47 changes: 42 additions & 5 deletions cookbook/ai_assistant_custome_tools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import os
import os, json
from typing import Optional, List
from phi.llm.openai.like import OpenAILike
from phi.assistant import Assistant
Expand All @@ -10,6 +10,8 @@
from phi.tools.email import EmailTools
from phi.utils.log import logger
from phi.tools.email import EmailTools
from phi.knowledge.base import AssistantKnowledge
from phi.knowledge.base import Document
from resources import vector_db
from rich.prompt import Prompt
from dotenv import load_dotenv
Expand All @@ -18,6 +20,32 @@
# To run this example, first make sure to follow the instructions below:
# 1. Install the phidata: pip install phidata
# 2. Run the following command to start a docker, with pgvector db running: phi start resources.py
# 3. Download the sample of JSON knowledge base from the same folder of this file: cinemax.json

class CinemaSerachDB(Toolkit):
def __init__(
self,
knowledge_base : Optional[AssistantKnowledge] = None,
num_documents: int = None
):
super().__init__(name="get_available_slots")
self.knowledge_base = knowledge_base
self.num_documents = num_documents
self.register(self.get_available_slots)

def get_available_slots(self, movie_slot_query: str ) -> str:
"""Use this function to search the Cinemax database of available movies, show time, and date.
:param query: The query to search the Cinemax database of available movies, show time, and date.
:return: A string containing the response to the query.
"""
relevant_docs: List[Document] = self.knowledge_base.search(query=movie_slot_query, num_documents=self.num_documents)
if len(relevant_docs) == 0:
return None

return json.dumps([doc.to_dict() for doc in relevant_docs], indent=2)



class CinemaTools(Toolkit):
def __init__(
Expand All @@ -29,7 +57,6 @@ def __init__(
self.register(self.book_cinema_ticket)

def book_cinema_ticket(self, movie_name: str, date: Optional[str] = None, time: Optional[str] = None, user_email: Optional[str] = None) -> str:
# """Books a cinema ticket for the given movie, date, and time, and sends an email to the user.
"""Use this function ONLY for booking a ticket, when all info is available (movie name, date, time and suer email). Do NOT use this function when user asks for movie details and other things
Args:
Expand Down Expand Up @@ -118,12 +145,22 @@ def cinemax_assistant(new: bool = False, user: str = "user"):
run_id=run_id,
user_id="test_user",
llm=my_groq,
knowledge_base=kb,
# knowledge_base=kb, Since I created my own tool CinemaSerachDB
storage=storage,
use_tools=True,
add_chat_history_to_messages=True,
tools=[CinemaTools(EmailTools("YOUR_EMAIL_ADDRESS", "SENDER_NAME", "SENDER_EMAIL", os.environ['email_pass_key'] ))], show_tool_calls=True, markdown=True
tools=[
CinemaSerachDB(knowledge_base=kb, num_documents=5),
CinemaTools(EmailTools("YOUR_EMAIL_ADDRESS", "SENDER_NAME", "SENDER_EMAIL", os.environ['email_pass_key'] ))
], show_tool_calls=True, markdown=True,
add_chat_history_to_messages=True, # Inject history in every prompt
add_datetime_to_instructions=True,
limit_tool_access=True,
extra_instructions="",
output_model= None, # Pydantic
prevent_prompt_injection=True,
add_references_to_prompt=False, # It searches in the kb and add it to every prompt
# add_references_to_prompt=True,
add_to_system_prompt="\n\nRemember:As a Cinemax agent, your responsibilities include: \n\n1. Assisting individuals in locating their preferred movies using the provided database information. \n2. Facilitating ticket booking through the tools at your disposal. \n3. If critical information is missing and the registration system returns an error, generate a polite, humanized version of the error message and request the necessary additional information from users. \n4. When interacting with users, never disclose that you are an AI model unable to perform specific functions or activate certain features. \n5. When users inquire about movie showtimes, available movies, or related details, utilize your tools to access data from the knowledge base, rather than relying on your own knowledge. However, you may provide plot information about specific movies in a concise and succinct manner, ensuring responses are not excessively lengthy. \n6.When engaging in a discussion with users about films and proposing movies based on their preferences and the available options, it would be beneficial to list the available dates and times tailored to the user's interests and conversation history. This approach would simplify the booking process for the user. If the user has already specified a date, it is essential to remember and adhere to it, avoiding the suggestion of alternative dates."
)
assistant.knowledge_base.load(recreate=False)

Expand Down
Loading

0 comments on commit d5ee25c

Please sign in to comment.