import uvicorn import datetime from flask import Flask, request, jsonify from huggingface_hub import InferenceClient app = Flask(__name__) API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1" def format_prompt(message, custom_instructions=None): prompt = "" if custom_instructions: prompt += f"[INST] {custom_instructions} [/INST]" prompt += f"[INST] {message} [/INST]" return prompt def Answer(Prompt, instructions, api, temperature=0.1, max_new_tokens=2, top_p=0.95, repetition_penalty=1.0): global API_URL try: temperature = float(temperature) if temperature < 1e-2: temperature = 1e-2 top_p = float(top_p) generate_kwargs = dict( temperature=temperature, max_new_tokens=max_new_tokens, top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=69, ) custom_instructions = instructions formatted_prompt = format_prompt(Prompt, custom_instructions) head = {"Authorization": f"Bearer {api}"} client = InferenceClient(API_URL, headers=head) response = client.text_generation(formatted_prompt, **generate_kwargs) return response except Exception as e: return str(e) def RealTime(Prompt, api): Realtime_intruction = f""" -> Today date = *{datetime.datetime.today().strftime("%d|%m|%y")}* Prompt: Create a system that analyzes user requests to determine if they are asking for real-time data. The system should accept a prompt and return realtime=true if the user is requesting real-time data and realtime=false if not. Assume that the user will explicitly mention their need for real-time data in the prompt. Examples: User: "What is the temperature in New York?" System Response: "realtime=true" User: "Show me yesterday's news headlines." System Response: "realtime=false" User: "Give me live updates on the stock market." System Response: "realtime=true" User: "Can you tell me the population of London?" System Response: "realtime=false" User: "who is our prime minister" System Response: "realtime=true" Requirements: The system should analyze the user's prompt and determine if it contains any indication of the need for real-time data. If the user explicitly mentions real-time data, the system should return realtime=true. If the user does not mention real-time data or indicates that it is not necessary, the system should return realtime=false. keywords => "current", "now" , "today's" and other's define them by your self """ Realtime_answer_formate = """ #reply only one of them ["True", "False"] Input : *{Question}* Output : """ return Answer(Prompt=Realtime_answer_formate.format(Question=Prompt),instructions=Realtime_intruction,api=api, temperature=0.1, max_new_tokens=2, top_p=0.95, repetition_penalty=1.0) def WhatItIs(Prompt:str, api:str): WhatIt_intruction = """ You are Decision Making Model. which select 2 options give below: -> 'Chatting' if the input is a question that chatbot should answer. -> 'Automation' if the input is an instruction to open or close anything in computer. Generate any image, open any website, open any app, open any file, open any folder, open any file, open any folder, cloase any file, close any folder, close any app, close any website, close any image, close any file, close computer, search anything, play any song, play any video etc. (whatever task that chat bot can perform) ***The output should be only one word*** Example (1) : hello can you open chrome for me? Example Output (1) : Automation Example (2) : who is akshay kumar? Example Output (2) : Chatting Example (3) : lets play a game. Example Output (2) : Chatting Example (2) : hello Example Output (2) : Chatting """ WhatItis_answer_Formate = """ #reply only one of them ["Automation", "Chatting"] Input : *{Question}* Output : """ return Answer(WhatItis_answer_Formate.format(Question=Prompt),instructions=WhatIt_intruction,api=api, temperature=0.1, max_new_tokens=2, top_p=0.95, repetition_penalty=1.0) @app.route("/agent/Realtime", methods=["POST"]) def Realtime_knowledge(): data = request.json Api = data.get("api_key") prompt = data.get("prompt") if not Api or not prompt: return jsonify({"error": "Missing required fields"}), 400 response = RealTime(str(prompt),str(Api)) return jsonify({"response": response}), 200 @app.route("/agent/think", methods=["POST"]) def Think_knowledge(): data = request.json Api = data.get("api_key") prompt = data.get("prompt") if not Api or not prompt: return jsonify({"error": "Missing required fields"}), 400 response = WhatItIs(str(prompt),str(Api)) return jsonify({"response": response}), 200 @app.route("/mistral/text-generation", methods=["POST"]) def Mistral_text_generation(): data = request.json prompt = data.get("prompt") instructions = data.get("instructions") api_key = data.get("api_key") if not prompt or not instructions or not api_key: return jsonify({"error": "Missing required fields"}), 400 response = Answer(str(prompt) , instructions=instructions , api=api_key) return jsonify({"response": response}), 200