Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New google colab pushed to main #187

Merged
merged 10 commits into from
Jan 13, 2025
Merged
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# OpenHosta
v2.1.4 - Open-Source Project

<a href="https://colab.research.google.com/github/hand-e-fr/OpenHosta/blob/doc/docs/openhosta_phi4.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/> Basic Usage - Synthetic Data - local LLM (phi-4)</a>
<br/>
<a href="https://colab.research.google.com/github/hand-e-fr/OpenHosta/blob/doc/docs/openhosta_agent.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/> Simple AI Agent with gpt-4o</a>


**- The future of development is human -**

Welcome to the OpenHosta documentation, a powerful tool that facilitates the integration LLM in the development environnement. OpenHosta is used to emulate functions using AI, while respecting Python's native paradygma and syntax.
Expand Down
257 changes: 257 additions & 0 deletions docs/openhosta_agent.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/hand-e-fr/OpenHosta/blob/doc/docs/openhosta_agent.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# OpenHosta Agent with a Local Phi-4 LLM\n",
"\n",
"This colab demonstrate simple use cases of OpenHosta. You need an OpenAI key to run it"
],
"metadata": {
"id": "2ywSpiksruBs"
}
},
{
"cell_type": "markdown",
"source": [
"## Basic Usage of AI Agents with OpenHosta"
],
"metadata": {
"id": "0x6AUM3osYK2"
}
},
{
"cell_type": "code",
"source": [
"!pip install OpenHosta"
],
"metadata": {
"id": "L9EUaVsR8x7x",
"outputId": "f8345a96-13c8-4737-9730-ad292079ffb4",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: OpenHosta in /usr/local/lib/python3.10/dist-packages (2.1.4)\n",
"Requirement already satisfied: requests>=2.32.3 in /usr/local/lib/python3.10/dist-packages (from OpenHosta) (2.32.3)\n",
"Requirement already satisfied: typing_extensions>=4.12.2 in /usr/local/lib/python3.10/dist-packages (from OpenHosta) (4.12.2)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.3->OpenHosta) (3.4.1)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.3->OpenHosta) (3.10)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.3->OpenHosta) (2.3.0)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.3->OpenHosta) (2024.12.14)\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"### Configure the LLM that you want to use"
],
"metadata": {
"id": "1ZEB71Ytsk4B"
}
},
{
"cell_type": "code",
"source": [
"from OpenHosta import config\n",
"\n",
"# If you have an OpenAI API key you can use it like this:\n",
"# Use default model (gpt-4o) through API key\n",
"# config.set_default_apiKey(<<YOUR API KEY>>)\n",
"\n",
"# Comment this if you use OpenAI models\n",
"# Use Microsoft local Phi-4 through ollama\n",
"# my_model=config.Model(\n",
"# base_url=\"http://localhost:11434/v1/chat/completions\",\n",
"# model=\"phi4\", api_key=\"none\", timeout=120\n",
"# )\n",
"# config.set_default_model(my_model)\n",
"\n",
"config.set_default_apiKey(\"<<YOU API KEY>>\") # Ask me one through LinkedIn, I will provide one for testing purpose!"
],
"metadata": {
"id": "SBC5-L0zqH1c"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Agent structure\n",
"\n",
"Ask a question then select best function to answer"
],
"metadata": {
"id": "T62JVKqU0rMg"
}
},
{
"cell_type": "markdown",
"source": [
"### Emulate functions using the seleted LLM"
],
"metadata": {
"id": "FauFveKWsp3G"
}
},
{
"cell_type": "code",
"source": [
"from OpenHosta import emulate"
],
"metadata": {
"id": "SEpo1gfJT8Bg"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Mix of python and Openhosta functions that the agent will use\n",
"def add(a:float, b:float)->float:\n",
" \"\"\"\n",
" add two numbers\n",
" \"\"\"\n",
" return a+b\n",
"\n",
"def number_to_string(number:float)->str:\n",
" \"\"\"\n",
" Convert a number to a string that represent the number in letters\n",
" \"\"\"\n",
" return emulate()\n",
"\n",
"def string_to_number(string:str)->float:\n",
" \"\"\"\n",
" Convert a string to a number\n",
" \"\"\"\n",
" return emulate()"
],
"metadata": {
"id": "icgBCk9j00-2"
},
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Agent main router\n",
"from typing import Dict, Literal, List, Tuple\n",
"\n",
"Actions = {\n",
" \"add\": add,\n",
" \"number_to_string\": number_to_string,\n",
" \"string_to_number\": string_to_number\n",
"}\n",
"ActionType = Literal[\"add\", \"number_to_string\", \"string_to_number\", \"Done\"]\n",
"\n",
"def find_best_step_to_execute(stack:List[Tuple[ActionType, list, str]], target)->Tuple[ActionType, list]:\n",
" \"\"\"\n",
" Select the best action to get elements needed to achieve target.\n",
"\n",
" You first print a strategy based on possible actions,\n",
" Then you look at already executed actions in stack,\n",
" Then you decide of the next action to take and its parameters\n",
"\n",
" :param stack: a list of already taken actions with (ActionType, list of params, Action returned value)\n",
" :param target: the overall target objective.\n",
"\n",
" :return: The next action to take and its parameters. Return Done if there is no more action to take\n",
" \"\"\"\n",
" return emulate()"
],
"metadata": {
"id": "gZ02NiW_1ooR"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Agent main loop"
],
"metadata": {
"id": "bVuS-CC6szQM"
}
},
{
"cell_type": "code",
"source": [
"request = \"can you add twenty two and hundred thousant fifity five and print the result in letters\"\n",
"\n",
"stack=[]\n",
"output = \"\"\n",
"\n",
"Done=False\n",
"while not Done:\n",
" next_action, params = find_best_step_to_execute(stack, request)\n",
" if next_action == \"Done\":\n",
" Done=True\n",
" else:\n",
" next_function = Actions[next_action]\n",
" print(f\"Executing {next_action} with params {params}\")\n",
" output = next_function(*params)\n",
" stack.append([next_action, params, output])\n",
"\n",
"print(output)\n"
],
"metadata": {
"id": "GXiKTdYm_GGp",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "9b04eaab-96f8-47ea-ceac-897362854481"
},
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Executing string_to_number with params ['twenty two']\n",
"Executing string_to_number with params ['hundred thousant fifity five']\n",
"Executing add with params [22.0, 100055.0]\n",
"Executing number_to_string with params [100077.0]\n",
"one hundred thousand seventy-seven\n"
]
}
]
}
]
}
Loading
Loading