用httpx写openai agent llm流式对话


import pyautogui
import os
import base64
import httpx
import asyncio
import json
from tools import *


api_key=os.environ.get("OPENAI_API_KEY", None)
api_url=os.environ.get("OPENAI_API_URL", None)
model_name=os.environ.get("MODEL_NAME", None)
if api_key is None or api_url is None or model_name is None:
  raise Exception("请设置环境变量 OPENAI_API_KEY、OPENAI_API_URL 和 MODEL_NAME。")
# 配置日志


       
async def send_to_llm(messages,tools:list =None)->tuple[str,list]:   
     
    # 发送 POST 请求
    async with httpx.AsyncClient() as client:
        
        response = await client.post(
            f"{api_url}/chat/completions",
            headers={
                "Content-Type": "application/json",
                "Authorization": f"Bearer {api_key}"
            },
        json={
            "model": model_name,
            "messages": messages,
            **({"tools": tools} if tools else {}),
            "stream": True,
            "temperature": 0.9 # 添加温度参数
        },
            timeout=None  # 禁用超时以支持流式读取
        )
        
        # 检查响应状态码
        if not response.is_success:
            raise Exception(f"LLM服务器错误: {response.status_code} - {response.text}")

        # 流式读取响应内容
        role=""
        full_response = ""
        action_pairs = []
        action_pair = {"name": "", "arguments": ""}

        async for chunk in response.aiter_text():
            lines = chunk.split("\n")
            for line in lines:
                if line.startswith("data: "):
                    if "[DONE]" in line:
                        continue

                    try:
                        data = json.loads(line[6:])
                        delta = data["choices"][0]["delta"]

                        # 更新角色信息
                        if delta.get("role"):
                            role = delta["role"]

                        # 更新内容
                        if delta.get("content"):
                            full_response += delta["content"]

                        # 处理工具调用
                        if delta.get("tool_calls"):
                            tool_call = delta["tool_calls"][0]["function"]
                            action_pair["arguments"] += tool_call["arguments"]

                            if tool_call.get("name"):
                                action_pair["name"] = tool_call["name"].strip()

                            # 如果参数完成,保存动作对
                            if "}" in action_pair["arguments"]:
                                action_pairs.append(action_pair)
                                action_pair = {"name": "", "arguments": ""}

                    except Exception as e:
                        print(f"Error parsing line: {line}, error: {e}")
                
            
        
           
            if tools :
                print("funcall模式,动作:",   action_pairs)
                print("-----------------------------------------------")
            else : print(role,":",full_response)
            return full_response,  action_pairs

            
def get_imageToBase64()->str:
    imagePath=os.path.join(os.path.expanduser('~'), '图片', 'screenshot.jpg')
# 获取屏幕的宽度和高度
    screen_width, screen_height = pyautogui.size()
    screenshot = pyautogui.screenshot(region=(60, 300, screen_width//2, screen_height-500))
    screenshot.save(imagePath)
    # 读取图片并转换为 Base64
    with open(imagePath, "rb") as image_file:
        base64_image = base64.b64encode(image_file.read()).decode("utf-8")
        return base64_image
def make_message( role: str,prompt: str, base64_image: str = None)->str:
    """
    :param prompt: 用户输入的文本提示
    :param base64_image: Base64 编码的图片数据(可选)
    """
    # 构造消息内容
    message =    {
            "role": role,
            "content": [
                {"type": "text", "text": prompt},
            ]
        }
    
    if base64_image:
        message["content"].append(
            {
                "type": "image_url",
                "image_url": {
                    "url": f"data:image/jpeg;base64,{base64_image}"
                }
            }
        )
    
    return message

async def operation(act: str ,text: str):
    try:
        # 定义目标 URL
        
        url = f"http://0.0.0.0:5878/{act}"
       
        # 发送 POST 请求
        async with httpx.AsyncClient() as client:
            response = await client.post( url,  json=json.loads(text) )
               
            
        # 检查响应状态码
        if not response.is_success:
            raise Exception("网络响应失败")
        else:
            return response.content.decode('utf-8') 
       # 假设你想打印返回的消息
        
    except Exception as error:
        print(f"调用接口时出错: {error}")
    
async def main():
  
    
    
    history = []
    while True:
        user_input=input("你:")

        
        user_promt=make_message("user",user_input)
        history.append(user_promt)
        
        if_fuctioncall,_=await send_to_llm([make_message("user",f"[{user_input}],分析这句话有没有发任务或者指令给你,有回答true,没有回答false")] )
   
         
        if 'true' in if_fuctioncall:
            
            _,actions=await send_to_llm([make_message("user",user_input+"\n可选方法:\n"+"\n".join(map(str, freecad_tools)))],freecad_tools)
            if actions:
                for action in actions:
                    responce= await operation(action["name"], action["arguments"])
                
                
                    history.append(make_message("system",responce))
  
        else:
            history_prompt ="\n".join([f"{message['role']}: {message['content'][0]}" for message in history[10:]])
            system_prompt=make_message("system",f"你是我的助手\n{history_prompt}")
            llm_response,_=await send_to_llm([system_prompt,user_promt])
           
            history.append(make_message("assistant",llm_response))
if __name__ == "__main__":
     asyncio.run(main())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值