Skip to main content

`AIGooFusion` is a framework for developing applications by large language models (LLMs)

Project description

python

AIGooFusion

AIGooFusion is a framework for developing applications by large language models (LLMs). AIGooFusion has AIGooChat and AIGooFlow.

  • AIGooChat is llm abstraction to use various llm on one module.
  • AIGooFlow is llm apps workflow.

How to install

Using pip

pip install aigoofusion

using requirements.txt

  • Add into requirements.txt
aigoofusion
  • Then install
pip install -r requirements.txt

Example

AIGooChat Example

info="""
Irufano adalah seorang software engineer.
Dia berasal dari Indonesia.
Kamu bisa mengunjungi websitenya di https:://irufano.github.io
""" 

def test_chat():
    # Configuration
    config = OpenAIConfig(
        temperature=0.7
    )

    # Initialize llm
    llm = OpenAIModel(model="gpt-4o-mini", config=config)
    
    SYSTEM_PROMPT = """Answer any user questions based solely on the data below:
    <data>
    {info}
    </data>
    
    DO NOT response outside context."""

    # Initialize framework
    framework = AIGooChat(llm, system_message=SYSTEM_PROMPT, input_variables=["info"])
    
    try:
        # Example conversation with tool use
        messages = [
            Message(role=Role.USER, content="apa ibukota indonesia?")
        ]
        with openai_usage_tracker() as usage:
            response = framework.generate(messages, info=info)
            print(f"\n>> {response.result.content}\n")
            print(f"\nUsage:\n{usage}\n")
        
    except AIGooException as e:
        print(f"{e}")

test_chat()

AIGooFlow Example

async def test_flow():
    # Configuration
    config = OpenAIConfig(
        temperature=0.7
    )

    llm = OpenAIModel("gpt-4o-mini", config)

    # Define a sample tool
    @Tool()
    def get_current_weather(location: str, unit: str = "celsius") -> str:
        return f"The weather in {location} is 22 degrees {unit}"

    @Tool()
    def get_current_time(location: str) -> str:
        # Initialize framework
        aig = AIGooChat(llm, system_message="You are a helpful assistant.")

        # Example conversation with tool use
        time = f"The time in {location} is 09:00 AM"
        msgs = [
            Message(role=Role.USER, content=time),
        ]
        res = aig.generate(msgs)
        return res.result.content or "No data"

    tool_list = [get_current_weather, get_current_time]

    # Initialize framework
    fmk = AIGooChat(llm, system_message="You are a helpful assistant.")

    # Register tool
    fmk.register_tool(tool_list)

    # Register to ToolRegistry
    tl_registry = ToolRegistry(tool_list)

    # Workflow
    workflow = AIGooFlow({
        "messages": [],
    })

    # Define Node functions
    async def main_agent(state: WorkflowState) -> dict:
        messages = state.get("messages", [])
        response = fmk.generate(messages)
        messages.append(response.process[-1])
        return {"messages": messages}

    async def tools(state: WorkflowState) -> dict:
        messages = tools_node(messages=state.get("messages", []), registry=tl_registry)
        return {"messages": messages}

    def should_continue(state: WorkflowState) -> str:
        messages = state.get("messages", [])
        last_message = messages[-1]
        if last_message.tool_calls:
            return "tools"
        return END


    # Add nodes
    workflow.add_node("main_agent", main_agent)
    workflow.add_node("tools", tools)

    # Add edges structure
    workflow.add_edge(START, "main_agent")
    workflow.add_conditional_edge("main_agent", ["tools", END], should_continue)
    workflow.add_edge("tools", "main_agent")

    async def call_sql_agent(question: str):
        try:
            with openai_usage_tracker() as usage:
                res = await workflow.execute({
                    "messages": [
                        Message(role=Role.USER, content=question)
                    ]
                })

            return res, usage
        except Exception as e:
            raise e


    quest="What's the weather like in London and what time is it?"
    res, usage = await call_sql_agent(quest)
    print(f"---\nResponse content:\n")
    print(res['messages'][-1].content)
    print(f"---\nRaw usages:")
    for usg in usage.raw_usages:
        print(f"{usg}")
    print(f"---\nCallback:\n {usage}")

async def run():
	# await test_workflow()
	await test_flow()

asyncio.run(run())

Sample In-memory messages

chat_memory = ChatMemory()

# Workflow
workflow = AIGooFlow({
	"messages": [] ,
})

async def main(state: WorkflowState) -> dict:
	messages = state.get("messages", [])
	responses = ["Hello", "Wowww", "Amazing", "Gokil", "Good game well played", "Selamat pagi", "Maaf aku tidak tahu"]
	random_answer = random.choice(responses)
	ai_message = Message(role=Role.ASSISTANT, content=random_answer)
	messages.append(ai_message)
	return {"messages": messages}


# Add nodes
workflow.add_node("main", main)
workflow.add_edge(START, "main")
workflow.add_edge("main", END)

async def call_workflow(question: str, thread_id: str):
	try:
		message = Message(role=Role.USER, content=question)

		async with chat_memory.intercept(thread_id=thread_id, message=message) as (messages, result_call):
			res = await workflow.execute({
				"messages": messages
			})
			# must call this back 
			result_call['messages'] = res['messages']

		history = chat_memory.get_thread_history(thread_id=thread_id, max_length=None)
		return res, history
	except Exception as e:
		raise e


async def chat_terminal():
	print("Welcome to the Chat Terminal! Type 'exit' to quit.")
	print("Use one digit number on thread id for simplicity testing, i.e: thread_id: 1")

	while True:
		thread_id = input("thread_id: ")
		user_input = input("You: ")

		if user_input.lower() == 'exit':
			print("Chatbot: Goodbye!")
			break

		response, history = await call_workflow(user_input.lower(), thread_id)
		time.sleep(0.5) # Simulate a small delay for realism
		print(f"\nChatbot: {response['messages'][-1].content}\n")
		print(f"History: ")
		for msg in history:
			print(f"\t{msg}")

if __name__ == "__main__":
	asyncio.run(chat_terminal())

Develop as Contributor

Build the container

docker-compose build

Run the container

docker-compose up -d aigoo-fusion

Stop the container

docker-compose stop aigoo-fusion

Access the container shell

docker exec -it aigoo_fusion bash

Run test

python aigoo_fusion/test/test_chat.py 
python aigoo_fusion/test/test_flow.py 

or

python aigoo_fusion.test.test_chat.py 
python aigoo_fusion.test.test_flow.py 

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aigoofusion-0.1.3.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aigoofusion-0.1.3-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file aigoofusion-0.1.3.tar.gz.

File metadata

  • Download URL: aigoofusion-0.1.3.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for aigoofusion-0.1.3.tar.gz
Algorithm Hash digest
SHA256 b0b0ecc1266e7a8f65cc3a77bcfe2061ab16c11f2d73911edd799852cc5d8280
MD5 f736e39b2a8bd29568c69e598d8a63c4
BLAKE2b-256 d0c04393ae4032be4d28287e6f0546ae4dbfc4ced5614a2017fc04ad4e4facac

See more details on using hashes here.

File details

Details for the file aigoofusion-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: aigoofusion-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 23.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for aigoofusion-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b2578f35db3fbc3a8ddf3b5db7ebc461f306b3bd5233b90fcfb4fdf12ab0ac4f
MD5 cb791f78e5a859cd10fa4f9484901c82
BLAKE2b-256 3341c0a87692fd56e446fb43a013aee525b50896cbb293b39f510a12c7bfed4b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page