Package: phidata
phidataFunction calling is all you needWhat is phidata?Phidata is a toolkit for building AI Assistants using function calling.Function calling enables LLMs to achieve tasks by calling functions and intelligently choosing their next step based on the response, just like how humans solve problems.How it worksStep 1:Create anAssistantStep 2:Add Tools (functions), Knowledge (vectordb) and Storage (database)Step 3:Serve using Streamlit, FastApi or Django to build your AI applicationInstallationpipinstall-UphidataExample 1: Assistant that can search the webCreate a fileassistant.pyfromphi.assistantimportAssistantassistant=Assistant(description="You help people with their health and fitness goals.")assistant.print_response("Share a quick healthy breakfast recipe.",markdown=True)Install openai and run theAssistantpipinstallopenai

pythonassistant.pyAddDuckDuckGofunctions to let theAssistantsearch the webfromphi.assistantimportAssistantfromphi.tools.duckduckgoimportDuckDuckGoassistant=Assistant(tools=[DuckDuckGo()],show_tool_calls=True)assistant.print_response("Whats happening in France?",markdown=True)Installduckduckgo-searchand run theAssistantpipinstallduckduckgo-search

pythonassistant.pyExample 2: Assistant that can write and run python codeThePythonAssistantcan achieve tasks using python code. Create a filepython_assistant.pyfromphi.assistant.pythonimportPythonAssistantfromphi.file.local.csvimportCsvFilepython_assistant=PythonAssistant(files=[CsvFile(path="https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv",description="Contains information about movies from IMDB.",)],pip_install=True,show_tool_calls=True,)python_assistant.print_response("What is the average rating of movies?",markdown=True)Install pandas and run thepython_assistant.pypipinstallpandas

pythonpython_assistant.pyExample 3: Assistant that can analyze data using SQLTheDuckDbAssistantcan perform data analysis using SQL. Create a filedata_assistant.pyimportjsonfromphi.assistant.duckdbimportDuckDbAssistantduckdb_assistant=DuckDbAssistant(semantic_model=json.dumps({"tables":[{"name":"movies","description":"Contains information about movies from IMDB.","path":"https://phidata-public.s3.amazonaws.com/demo_data/IMDB-Movie-Data.csv",}]}),)duckdb_assistant.print_response("What is the average rating of movies? Show me the SQL.",markdown=True)Install duckdb and run thedata_assistant.pyfilepipinstallduckdb

pythondata_assistant.pyDemosCheckout these AI apps showcasing the advantage of function calling:PDF AIthat summarizes and answers questions from PDFs.ArXiv AIthat answers questions about ArXiv papers using the ArXiv API.HackerNews AIthat interacts with the HN API to summarize stories, users, find out what's trending, summarize topics.Demo Streamlit Appserving a PDF, Image and Website Assistant (password: admin)Demo FastApiserving a PDF Assistant.TutorialsBuild an AI App in 3 stepsBuild a Local RAG AI App using OpenHermes and OllamaExamplesAssistant that calls the HackerNews APICreate a fileapi_assistant.pythat can call the HackerNews API to get top stories.importjsonimporthttpxfromphi.assistantimportAssistantdefget_top_hackernews_stories(num_stories:int=10)->str:"""Use this function to get top stories from Hacker News.Args:num_stories (int): Number of stories to return. Defaults to 10.Returns:str: JSON string of top stories."""# Fetch top story IDsresponse=httpx.get('https://hacker-news.firebaseio.com/v0/topstories.json')story_ids=response.json()# Fetch story detailsstories=[]forstory_idinstory_ids[:num_stories]:story_response=httpx.get(f'https://hacker-news.firebaseio.com/v0/item/{story_id}.json')story=story_response.json()if"text"instory:story.pop("text",None)stories.append(story)returnjson.dumps(stories)assistant=Assistant(tools=[get_top_hackernews_stories],show_tool_calls=True)assistant.print_response("Summarize the top stories on hackernews?",markdown=True)Run theapi_assistant.pyfilepythonapi_assistant.pySee it work through the problem╭──────────┬───────────────────────────────────────────────────────────────────╮
│Message│Summarizethetopstoriesonhackernews?│
├──────────┼───────────────────────────────────────────────────────────────────┤
│Response││
│(51.1s)│•Running:get_top_hackernews_stories(num_stories=5)│
│││
││Here's a summary of the top stories on Hacker News:               ││          │                                                                   ││          │  1 Boeing Whistleblower: Max 9 Production Line Has "Enormous      ││          │    Volume of Defects" A whistleblower has revealed that Boeing's│
││Max9productionlineisriddledwithan"enormous volume of   ││          │    defects,"withinstanceswhereboltswerenotinstalled.The│
││storyhasgarneredattentionwithascoreof140.Readmore│
││2ArnoA.Penzias,90,Dies;NobelPhysicistConfirmedBigBang│
││TheoryArnoA.Penzias,aNobelPrize-winningphysicistknown│
││forhisworkthatconfirmedtheBigBangTheory,haspassed│
││awayattheageof90.Hiscontributionstosciencehavebeen│
││significant,leadingtodiscussionsandtributesinthe│
││scientificcommunity.Thenewshasascoreof207.Readmore│
││3WhythefuckarewetemplatingYAML?(2019)Thisprovocative│
││articlefrom2019questionstheproliferationofYAML│
││templatinginsoftware,sparkingalargerconversationabout│
││thecomplexitiesandpotentialpitfallsofthispractice.With│
││asubstantialscoreof149,itremainsahottopicofdebate.│
││Readmore│
││4ForgingsignedcommitsonGitHubResearchershavediscovereda│
││methodforforgingsignedcommitsonGitHubwhichiscausing│
││concernwithinthetechcommunityabouttheimplicationsfor│
││codesecurityandintegrity.Thestoryhasacurrentscoreof│
││94.Readmore│
││5Qdrant,theVectorSearchDatabase,raised$28MinaSeriesA│
││roundQdrant,acompanyspecializinginvectorsearch│
││databases,hassuccessfullyraised$28millioninaSeriesA│
││fundinground.Thisfinancialmilestoneindicatesgrowing│
││interestandconfidenceintheirtechnology.Thestoryhas│
││attractedattentionwithascoreof55.Readmore│
╰──────────┴───────────────────────────────────────────────────────────────────╯Assistant that generates pydantic modelsOne of our favorite features is generating structured data (i.e. a pydantic model) from sparse information.
Meaning we can use Assistants to return pydantic models and generate content which previously could not be possible.
In this example, our movie assistant generates an object of theMovieScriptclass.Create a filepydantic_assistant.pyfromtypingimportListfrompydanticimportBaseModel,Fieldfromrich.prettyimportpprintfromphi.assistantimportAssistantclassMovieScript(BaseModel):setting:str=Field(...,description="Provide a nice setting for a blockbuster movie.")ending:str=Field(...,description="Ending of the movie. If not available, provide a happy ending.")genre:str=Field(...,description="Genre of the movie. If not available, select action, thriller or romantic comedy.")name:str=Field(...,description="Give a name to this movie")characters:List[str]=Field(...,description="Name of characters for this movie.")storyline:str=Field(...,description="3 sentence storyline for the movie. Make it exciting!")movie_assistant=Assistant(description="You help people write movie ideas.",output_model=MovieScript,)pprint(movie_assistant.run("New York"))Run thepydantic_assistant.pyfilepythonpydantic_assistant.pySee how the assistant generates a structured outputMovieScript(│setting='A bustling and vibrant New York City',
│ending='The protagonist saves the city and reconciles with their estranged family.',
│genre='action',
│name='City Pulse',
│characters=['Alex Mercer','Nina Castillo','Detective Mike Johnson'],
│storyline='In the heart of New York City, a former cop turned vigilante, Alex Mercer, teams up with a street-smart activist, Nina Castillo, to take down a corrupt political figure who threatens to destroy the city. As they navigate through the intricate web of power and deception, they uncover shocking truths that push them to the brink of their abilities. With time running out, they must race against the clock to save New York and confront their own demons.')A PDF Assistant with Knowledge & StorageLets create a PDF Assistant that can answer questions from a PDF. We'll usePgVectorfor knowledge and storage.Knowledge Base:information that the Assistant can search to improve its responses (uses a vector db).Storage:provides long term memory for Assistants (uses a database).Run PgVectorInstalldocker desktopfor running PgVector in a container.Create a fileresources.pywith the following contentsfromphi.docker.app.postgresimportPgVectorDbfromphi.docker.resourcesimportDockerResources# -*- PgVector running on port 5432:5432vector_db=PgVectorDb(pg_user="ai",pg_password="ai",pg_database="ai",debug_mode=True,)# -*- DockerResourcesdev_docker_resources=DockerResources(apps=[vector_db])StartPgVectorusingphistartresources.py-yCreate PDF AssistantCreate a filepdf_assistant.pyimporttyperfromrich.promptimportPromptfromtypingimportOptional,Listfromphi.assistantimportAssistantfromphi.storage.assistant.postgresimportPgAssistantStoragefromphi.knowledge.pdfimportPDFUrlKnowledgeBasefromphi.vectordb.pgvectorimportPgVector2fromresourcesimportvector_dbknowledge_base=PDFUrlKnowledgeBase(urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],vector_db=PgVector2(collection="recipes",db_url=vector_db.get_db_connection_local(),),)# Comment out after first runknowledge_base.load(recreate=False)storage=PgAssistantStorage(table_name="pdf_assistant",db_url=vector_db.get_db_connection_local(),)defpdf_assistant(new:bool=False,user:str="user"):run_id:Optional[str]=Noneifnotnew:existing_run_ids:List[str]=storage.get_all_run_ids(user)iflen(existing_run_ids)>0:run_id=existing_run_ids[0]assistant=Assistant(run_id=run_id,user_id=user,knowledge_base=knowledge_base,storage=storage,# use_tools=True adds functions to# search the knowledge base and chat historyuse_tools=True,show_tool_calls=True,# Uncomment the following line to use traditional RAG# add_references_to_prompt=True,)ifrun_idisNone:run_id=assistant.run_idprint(f"Started Run:{run_id}\n")else:print(f"Continuing Run:{run_id}\n")whileTrue:message=Prompt.ask(f"[bold] :sunglasses:{user}[/bold]")ifmessagein("exit","bye"):breakassistant.print_response(message,markdown=True)if__name__=="__main__":typer.run(pdf_assistant)Install librariespipinstall-UpgvectorpypdfpsycopgsqlalchemyRun PDF Assistantpythonpdf_assistant.pyAsk a question:How do I make pad thai?See how the Assistant searches the knowledge base and returns a response.Show outputStartedRun:d28478ea-75ed-4710-8191-22564ebfb140

INFOLoadingknowledgebase
INFOReading:https://www.family-action.org.uk/content/uploads/2019/07/meals-more-recipes.pdf
INFOLoaded82documentstoknowledgebase😎user:HowdoImakechickentikkasalad?
╭──────────┬─────────────────────────────────────────────────────────────────────────────────╮
│Message│HowdoImakechickentikkasalad?│
├──────────┼─────────────────────────────────────────────────────────────────────────────────┤
│Response││
│(7.2s)│•Running:search_knowledge_base(query=chickentikkasalad)│
│││
││IfoundarecipeforChickenTikkaSaladthatserves2.Herearethe│
││ingredientsandsteps:│
│││
││Ingredients:│

...Messagebyeto exit, start the assistant again usingpython pdf_assistant.pyand ask:What was my last message?See how the assistant now maintains storage across sessions.Run thepdf_assistant.pyfile with the--newflag to start a new run.pythonpdf_assistant.py--newStop PgVectorPlay around and then stopPgVectorusingphi stop resources.pyphistopresources.py-yBuild an AI App using Streamlit, FastApi and PgVectorLet's build anAI Appusing GPT-4 as the LLM, Streamlit as the chat interface, FastApi as the API and PgVector for knowledge and storage. Read the full tutorialhere.Create your codebaseCreate your codebase using theai-apptemplatephiwscreate-tai-app-nai-appThis will create a folderai-appwith a pre-built AI App that you can customize and make your own.Serve your App using StreamlitStreamlitallows us to build micro front-ends and is extremely useful for building basic applications in pure python. Start theappgroup using:phiwsup--groupappPress Enterto confirm and give a few minutes for the image to download.PDF AssistantOpenlocalhost:8501to view streamlit apps that you can customize and make your own.Click onPDF Assistantin the sidebarEnter a username and wait for the knowledge base to load.Choose either theRAGorAutonomousAssistant type.Ask "How do I make pad thai?"Upload PDFs and ask questionsWe provide a default PDF of ThaiRecipes that you can clear using theClear Knowledge Basebutton. The PDF is only for testing.Optional: Serve your App using FastApiStreamlit is great for building micro front-ends but any production application will be built using a front-end framework likenext.jsbacked by a RestApi built using a framework likeFastApi.Your AI App comes ready-to-use with FastApi endpoints.Update theworkspace/settings.pyfile and setdev_api_enabled=True...ws_settings=WorkspaceSettings(...# Uncomment the following linedev_api_enabled=True,...Start theapigroup using:phiwsup--groupapiPress Enterto confirm and give a few minutes for the image to download.View API EndpointsOpenlocalhost:8000/docsto view the API Endpoints.Load the knowledge base using/v1/assitants/load-knowledge-baseTest thev1/assitants/chatendpoint with{"message": "How do I make chicken curry?"}The Api comes pre-built with endpoints that you can integrate with your front-end.Optional: Run JupyterlabA jupyter notebook is a must-have for AI development and yourai-appcomes with a notebook pre-installed with the required dependencies. Enable it by updating theworkspace/settings.pyfile:...ws_settings=WorkspaceSettings(...# Uncomment the following linedev_jupyter_enabled=True,...Startjupyterusing:phiwsup--groupjupyterPress Enterto confirm and give a few minutes for the image to download (only the first time). Verify container status and view logs on the docker dashboard.View Jupyterlab UIOpenlocalhost:8888to view the Jupyterlab UI. Password:adminPlay around with cookbooks in thenotebooksfolder.Delete local resourcesStop the workspacePlay around and stop the workspace using:phiwsdownRun your AI App on AWSRead how torun your AI App on AWS.Checkout the cookbook for more examplesDocumentationYou can find the full documentationhereYou can also chat with us ondiscordOr email us athelp@phidata.comAI ApplicationsAfter building an Assistant, serve it usingStreamlit,FastApiorDjangoto build your AI application.
Instead of wiring tools manually, phidata providespre-builttemplates for AI Apps that you can run locally or deploy to AWS with 1 command. Here's how they work:Create your AI App using a template:phi ws createRun your app locally:phi ws upRun your app on AWS:phi ws up prd:awsBuilding AI for your product?We've helped many companies build AI for their products, the general workflow is:Train an assistantwith proprietary data to perform tasks specific to your product.Connect your productto the assistant via an API.Customize, Monitor and Improvethe AI.We provide dedicated support and development for AI products.Book a callto get started.ContributionsWe're an open-source project and welcome contributions, please read thecontributing guidefor more information.Request a featureIf you have a feature request, please open an issue or make a pull request.If you have ideas on how we can improve, please create a discussion.RoadmapOur roadmap is availablehere.
If you have a feature request, please open an issue/discussion.