Skip to content

Latest commit

 

History

History
154 lines (111 loc) · 4.39 KB

quickstart.mdx

File metadata and controls

154 lines (111 loc) · 4.39 KB
title description mode
Quickstart Guide
Get up and running with SmartGraph in minutes
wide

Welcome to SmartGraph! This guide will help you set up your first SmartGraph application in just a few minutes. We'll create a simple AI assistant that can answer questions and perform web searches.

Prerequisites

  • Python 3.11 or higher
  • pip (Python package installer)

Installation

First, let's install SmartGraph and its dependencies. Open your terminal and run:

pip install smartgraph python-dotenv
Create a new directory for your project and navigate into it:
mkdir my_smartgraph_app
cd my_smartgraph_app

Create a new file called .env in this directory and add your OpenAI API key:

OPENAI_API_KEY=your_api_key_here

Replace your_api_key_here with your actual OpenAI API key.

Create a new file called `app.py` and add the following code:
import asyncio
import os

from dotenv import load_dotenv

from smartgraph import ReactiveSmartGraph
from smartgraph.components import CompletionComponent, TextInputHandler
from smartgraph.tools.duckduckgo_toolkit import DuckDuckGoToolkit

# Load environment variables
load_dotenv()

class SmartAssistant(ReactiveSmartGraph):
    def __init__(self):
        super().__init__()
        pipeline = self.create_pipeline("Assistant")

        pipeline.add_component(TextInputHandler("Input"))
        pipeline.add_component(CompletionComponent(
            "AIAssistant",
            model="gpt-4o-mini",
            api_key=os.getenv("OPENAI_API_KEY"),
            system_context="You are a helpful assistant that can answer questions and perform web searches when needed.",
            toolkits=[DuckDuckGoToolkit()]
        ))

        self.compile()

    async def ask(self, question: str):
        return await self.execute_and_await("Assistant", question)

async def main():
    assistant = SmartAssistant()

    while True:
        user_input = input("Ask me anything (or type 'exit' to quit): ")
        if user_input.lower() == 'exit':
            break

        response = await assistant.ask(user_input)
        print(f"Assistant: {response.get('ai_response', 'Sorry, I could not generate a response.')}")
if __name__ == "__main__":
    asyncio.run(main())

This script sets up a simple AI assistant that can answer questions and perform web searches when needed.

Now, let's run your SmartGraph application:

python app.py

You should see a prompt asking you to input a question. Try asking various questions, including some that might require a web search.

Example interaction:

Ask me anything (or type 'exit' to quit): What is the capital of France?
Assistant: The capital of France is Paris.

Ask me anything (or type 'exit' to quit): Who won the latest Nobel Prize in Literature?
Assistant: I'll need to search for the most up-to-date information on that. One moment please...

The latest Nobel Prize in Literature was awarded to Jon Fosse in 2023. Jon Fosse is a Norwegian author known for his minimalist style and work in various genres including novels, poetry, essays, children's books, and drama. He was awarded the prize "for his innovative plays and prose which give voice to the unsayable."

Ask me anything (or type 'exit' to quit): exit
## What's Next?

Congratulations! You've just created your first SmartGraph application. This simple example demonstrates the power of SmartGraph in creating reactive, AI-powered applications.

To learn more and expand your SmartGraph skills, check out these resources:

Understand the fundamental building blocks of SmartGraph Learn how to create more complex workflows Dive deeper into creating sophisticated AI assistants Extend SmartGraph's functionality with your own components

Happy coding with SmartGraph!