Saturday, August 10, 2024

Functions to hook my workflows into Groq API and the Hugging Face API

This is the Grok API function to hook into my workflow.


from groq import Groq
def groq_text_completion (groq_request:str,groq_model:str, groq_api_key:str, output:list)->str:

client = Groq(
api_key=groq_api_key,
)

chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": groq_request,
}
],
model=groq_model,
)

return {output[0]: chat_completion.choices[0].message.content}, {"status": {"value": 0, "reason": "Success"}}

This is the code to hook my workflow into Hugging Face API for text completions. 


from hugchat import hugchat
from hugchat.login import Login
def hugging_text_completion(hugging_request:str,hugging_model:int, hugging_user_name:str, hugging_password:str, output:list)->str:

# Log in to huggingface and grant authorization to huggingchat
cookie_path_dir = "./cookies/" # NOTE: trailing slash (/) is required to avoid errors
sign = Login(hugging_user_name, hugging_password)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)

chatbot = hugchat.ChatBot(cookies=cookies.get_dict(), default_llm=hugging_model) # or cookie_path="usercookies/<email>.json"
message_result = chatbot.chat(hugging_request) # note: message_result is a generator, the method will return immediately.

return {output[0]: message_result}, {"status": {"value": 0, "reason": "Success"}}


These two example show just how easy it is to connect an entire cloud API into my workflow system and then be able to use those API's to do text completions for a workflow. :D 

There is the agent definitions to connect the agent to the function. Eventually the code will be stored directly inside the agent definition in the config file. I am doing it this way because there will eventually be hundreds of api connections and I want people to use simple bullet proof functions to connect to all those APIs, both local and remote.


        "groq_text_completion": {

            "type": "proc",

            "help": "make a groq chat text completion",

            "function": "groq_text_completion",

            "function_def": "",

            "inputs": [

                "groq_request",

                "groq_model",

                "groq_api_key"

            ],

            "optional_inputs": [],

            "outputs": [

                "response"

            ]

        },

        "get_groq_response": {

            "type": "workflow",

            "help": "Given a prompt, get the ollama response.",

            "inputs": [

                "prompt"

            ],

            "optional_inputs": [],

            "outputs": [

                "response"

            ],

            "return_on_fail": 1,

            "steps": [

                {

                    "agent": "groq_text_completion",

                    "params": {

                        "groq_request": "$prompt",

                        "groq_model": "llama3-8b-8192",

                        "groq_api_key": "Your api key"

                    },

                    "output": [

                        "response"

                    ]

                }

            ]

        },


        "hugging_text_completion": {

            "type": "proc",

            "help": "make a hugging chat text completion.",

            "function": "hugging_text_completion",

            "function_def": "",

            "inputs": [

                "hugging_request",

                "hugging_model",

                "hugging_user_name",

                "hugging_password"

            ],

            "optional_inputs": [],

            "outputs": [

                "response"

            ]

        },

        "get_hugging_response": {

            "type": "workflow",

            "help": "Given a prompt, get the response.",

            "inputs": [

                "prompt"

            ],

            "optional_inputs": [],

            "outputs": [

                "response"

            ],

            "return_on_fail": 1,

            "steps": [

                {

                    "agent": "hugging_text_completion",

                    "params": {

                        "hugging_request": "$prompt",

                        "hugging_model": 1,

                        "hugging_user_name": "your email",

                        "hugging_password": "your password"

                    },

                    "output": [

                        "response"

                    ]

                }

            ]

        }, 

No comments:

Post a Comment