api-for-open-llm icon indicating copy to clipboard operation
api-for-open-llm copied to clipboard

InternLM 20B 胡言乱语,什么原因?

Open jaffe-fly opened this issue 1 year ago • 0 comments
trafficstars

提交前必须检查以下项目 | The following items must be checked before submission

  • [X] 请确保使用的是仓库最新代码(git pull),一些问题已被解决和修复。 | Make sure you are using the latest code from the repository (git pull), some issues have already been addressed and fixed.
  • [X] 我已阅读项目文档FAQ章节并且已在Issue中对问题进行了搜索,没有找到相似问题和解决方案 | I have searched the existing issues / discussions

问题类型 | Type of problem

模型推理和部署 | Model inference and deployment

操作系统 | Operating system

Linux

详细描述问题 | Detailed description of the problem

client = OpenAI(
    api_key="EMPTY",
    base_url="http://xxxxxxx:10002/v1/",
)



def create_uuid():
    return str(uuid.uuid4())


def get_glm4(message):
    tools = [
        {
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "获取目标地点的天气信息",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "city": {
                            "type": "string",
                            "description": "目标地点的名称,必须是中文",
                        },
                    },
                    "required": ["city"],
                }
            }
        },
    ]

    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=message,
        tools=tools,
        tool_choice="auto",
        temperature=0.8,
        top_p=0.8,
        )
    return response.choices[0].message


def get_location_coordinate(location, city):
    url = f"https://restapi.amap.com/v5/place/text?key={gaode_key}&keywords={location}&region={city}"
    r = requests.get(url)
    result = r.json()
    if "pois" in result and result["pois"]:
        return result["pois"][0]
    return None


def search_nearby_pois(longitude, latitude, keyword):
    url = f"https://restapi.amap.com/v5/place/around?key={gaode_key}&keywords={keyword}&location={longitude},{latitude}"
    r = requests.get(url)
    result = r.json()
    ans = ""
    if "pois" in result and result["pois"]:
        for i in range(min(3, len(result["pois"]))):
            name = result["pois"][i]["name"]
            address = result["pois"][i]["address"]
            distance = result["pois"][i]["distance"]
            ans += f"{name}\n{address}\n距离:{distance}米\n\n"
    return ans


def get_weather_information(city):
    url = 'https://restapi.amap.com/v3/weather/weatherInfo?parameters'
    data = {
        "key": gaode_key,
        "city": city
    }
    r = requests.get(url, params=data)
    result = r.json()
    if "lives" in result and result["lives"]:
        return result["lives"][0]
    return None


def chat_but(input_):
    messages.append({"role": "user", "content": input_})
    response = get_glm4(messages)
    if response.tool_calls is None:
        messages.append({"role": response.role, "content": response.content})
    else:
        messages.append({"role": response.role, "content": response.tool_calls[0].function.arguments})
    while response.tool_calls is not None:
        print(response.tool_calls)
        for tool_call in response.tool_calls:
            args = json.loads(tool_call.function.arguments)
            result = ''
            if tool_call.function.name == "get_location_coordinate":
                result = get_location_coordinate(**args)
            elif tool_call.function.name == "search_nearby_pois":
                result = search_nearby_pois(**args)
            elif tool_call.function.name == "get_weather_information":
                result = get_weather_information(**args)
            if response.tool_calls is None:
                messages.append({
                    "tool_call_id": tool_call.id,
                    "role": "tool",
                    "name": tool_call.function.name,
                    "content": result
                })
            else:
                messages.append({"role": response.role, "content": response.tool_calls[0].function.arguments})
        response = get_glm4(messages)
        if response.tool_calls is None:
            messages.append({"role": response.role, "content": response.content})
        else:
            messages.append({"role": response.role, "content": response.tool_calls[0].function.arguments})
    return response.content


messages = [{"role": "system",
             "content": "你是书生浦语2,一个无害的人工智能助手"}]

print(chat_but('我想了解今天上海的天气'))
print(messages)

env配置

# model related
MODEL_NAME=internlm2
MODEL_PATH=/home/xxxxx/InternLM/internlm2-chat-20b-quant4
EMBEDDING_NAME=
ADAPTER_MODEL_PATH=
QUANTIZE=4
CONTEXT_LEN=
LOAD_IN_8BIT=false
LOAD_IN_4BIT=false
USING_PTUNING_V2=false
STREAM_INTERVERL=2
PROMPT_NAME=

# device related
DEVICE=

# "auto", "cuda:0", "cuda:1", ...
DEVICE_MAP=auto
GPUS=
NUM_GPUs=1
DTYPE=half


# api related
API_PREFIX=/v1

USE_STREAMER_V2=false
ENGINE=default

Dependencies

# 请在此处粘贴依赖情况
# Please paste the dependencies here

运行日志或截图 | Runtime logs or screenshots

接口日志 image 接口返回: image

jaffe-fly avatar Mar 08 '24 10:03 jaffe-fly