2024-12-06 10:28:35 +08:00
|
|
|
import base64
|
2024-10-18 16:50:02 +01:00
|
|
|
import copy
|
2024-10-18 14:17:14 +01:00
|
|
|
import json
|
2024-12-06 10:28:35 +08:00
|
|
|
import os
|
2024-12-13 19:57:25 +08:00
|
|
|
import re
|
2024-12-06 10:28:35 +08:00
|
|
|
import struct
|
|
|
|
from functools import lru_cache
|
2024-12-13 16:18:33 +08:00
|
|
|
from typing import List, Dict, Callable, Any, Union, Optional
|
2024-10-18 14:17:14 +01:00
|
|
|
import aioboto3
|
2024-10-22 15:16:57 +08:00
|
|
|
import aiohttp
|
2024-10-10 15:02:30 +08:00
|
|
|
import numpy as np
|
2024-10-16 15:15:10 +08:00
|
|
|
import ollama
|
2024-12-06 10:28:35 +08:00
|
|
|
import torch
|
2024-10-25 13:32:25 +05:30
|
|
|
from openai import (
|
|
|
|
AsyncOpenAI,
|
|
|
|
APIConnectionError,
|
|
|
|
RateLimitError,
|
|
|
|
Timeout,
|
|
|
|
AsyncAzureOpenAI,
|
|
|
|
)
|
2024-12-06 10:28:35 +08:00
|
|
|
from pydantic import BaseModel, Field
|
2024-10-10 15:02:30 +08:00
|
|
|
from tenacity import (
|
|
|
|
retry,
|
|
|
|
stop_after_attempt,
|
|
|
|
wait_exponential,
|
|
|
|
retry_if_exception_type,
|
|
|
|
)
|
2024-10-19 09:43:17 +05:30
|
|
|
from transformers import AutoTokenizer, AutoModelForCausalLM
|
2024-12-06 10:28:35 +08:00
|
|
|
|
2024-11-29 21:41:37 +08:00
|
|
|
from .utils import (
|
|
|
|
wrap_embedding_func_with_attrs,
|
|
|
|
locate_json_string_body_from_string,
|
2024-12-11 14:06:55 +08:00
|
|
|
safe_unicode_decode,
|
|
|
|
logger,
|
2024-11-29 21:41:37 +08:00
|
|
|
)
|
2024-10-19 09:43:17 +05:30
|
|
|
|
2024-12-06 08:48:55 +08:00
|
|
|
import sys
|
|
|
|
|
|
|
|
if sys.version_info < (3, 9):
|
|
|
|
from typing import AsyncIterator
|
|
|
|
else:
|
|
|
|
from collections.abc import AsyncIterator
|
|
|
|
|
2024-10-14 19:41:07 +08:00
|
|
|
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
2024-10-19 09:43:17 +05:30
|
|
|
|
|
|
|
|
2024-10-10 15:02:30 +08:00
|
|
|
@retry(
|
|
|
|
stop=stop_after_attempt(3),
|
|
|
|
wait=wait_exponential(multiplier=1, min=4, max=10),
|
|
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)),
|
|
|
|
)
|
|
|
|
async def openai_complete_if_cache(
|
2024-10-19 09:43:17 +05:30
|
|
|
model,
|
|
|
|
prompt,
|
|
|
|
system_prompt=None,
|
|
|
|
history_messages=[],
|
|
|
|
base_url=None,
|
|
|
|
api_key=None,
|
|
|
|
**kwargs,
|
2024-10-10 15:02:30 +08:00
|
|
|
) -> str:
|
2024-10-15 12:55:05 -07:00
|
|
|
if api_key:
|
|
|
|
os.environ["OPENAI_API_KEY"] = api_key
|
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
openai_async_client = (
|
|
|
|
AsyncOpenAI() if base_url is None else AsyncOpenAI(base_url=base_url)
|
|
|
|
)
|
2024-12-08 17:35:52 +08:00
|
|
|
kwargs.pop("hashing_kv", None)
|
2024-12-10 09:52:27 +08:00
|
|
|
kwargs.pop("keyword_extraction", None)
|
2024-10-10 15:02:30 +08:00
|
|
|
messages = []
|
|
|
|
if system_prompt:
|
|
|
|
messages.append({"role": "system", "content": system_prompt})
|
|
|
|
messages.extend(history_messages)
|
|
|
|
messages.append({"role": "user", "content": prompt})
|
2024-12-06 10:21:53 +08:00
|
|
|
|
2024-12-11 12:45:10 +08:00
|
|
|
# 添加日志输出
|
|
|
|
logger.debug("===== Query Input to LLM =====")
|
|
|
|
logger.debug(f"Query: {prompt}")
|
|
|
|
logger.debug(f"System prompt: {system_prompt}")
|
|
|
|
logger.debug("Full context:")
|
2024-11-30 00:00:51 +08:00
|
|
|
if "response_format" in kwargs:
|
|
|
|
response = await openai_async_client.beta.chat.completions.parse(
|
|
|
|
model=model, messages=messages, **kwargs
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
response = await openai_async_client.chat.completions.create(
|
|
|
|
model=model, messages=messages, **kwargs
|
|
|
|
)
|
2024-12-06 08:17:20 +08:00
|
|
|
|
2024-12-07 14:41:09 +08:00
|
|
|
if hasattr(response, "__aiter__"):
|
2024-12-06 14:29:16 +08:00
|
|
|
|
2024-12-07 14:41:09 +08:00
|
|
|
async def inner():
|
|
|
|
async for chunk in response:
|
|
|
|
content = chunk.choices[0].delta.content
|
|
|
|
if content is None:
|
|
|
|
continue
|
|
|
|
if r"\u" in content:
|
2024-12-09 19:14:27 +08:00
|
|
|
content = safe_unicode_decode(content.encode("utf-8"))
|
2024-12-07 14:41:09 +08:00
|
|
|
yield content
|
|
|
|
|
|
|
|
return inner()
|
|
|
|
else:
|
|
|
|
content = response.choices[0].message.content
|
|
|
|
if r"\u" in content:
|
2024-12-09 19:14:27 +08:00
|
|
|
content = safe_unicode_decode(content.encode("utf-8"))
|
2024-12-07 14:41:09 +08:00
|
|
|
return content
|
2024-10-10 15:02:30 +08:00
|
|
|
|
2024-10-18 16:50:02 +01:00
|
|
|
|
2024-10-21 20:40:49 +02:00
|
|
|
@retry(
|
|
|
|
stop=stop_after_attempt(3),
|
|
|
|
wait=wait_exponential(multiplier=1, min=4, max=10),
|
|
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)),
|
|
|
|
)
|
2024-10-25 13:32:25 +05:30
|
|
|
async def azure_openai_complete_if_cache(
|
|
|
|
model,
|
2024-10-21 20:40:49 +02:00
|
|
|
prompt,
|
|
|
|
system_prompt=None,
|
|
|
|
history_messages=[],
|
|
|
|
base_url=None,
|
|
|
|
api_key=None,
|
2024-11-30 17:47:33 +01:00
|
|
|
api_version=None,
|
2024-10-25 13:32:25 +05:30
|
|
|
**kwargs,
|
|
|
|
):
|
2024-10-21 20:40:49 +02:00
|
|
|
if api_key:
|
|
|
|
os.environ["AZURE_OPENAI_API_KEY"] = api_key
|
|
|
|
if base_url:
|
|
|
|
os.environ["AZURE_OPENAI_ENDPOINT"] = base_url
|
2024-11-30 17:47:33 +01:00
|
|
|
if api_version:
|
|
|
|
os.environ["AZURE_OPENAI_API_VERSION"] = api_version
|
2024-10-21 20:40:49 +02:00
|
|
|
|
2024-10-25 13:32:25 +05:30
|
|
|
openai_async_client = AsyncAzureOpenAI(
|
|
|
|
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
|
|
|
|
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
|
|
|
|
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
|
|
|
|
)
|
2024-12-08 17:35:52 +08:00
|
|
|
kwargs.pop("hashing_kv", None)
|
2024-10-21 20:40:49 +02:00
|
|
|
messages = []
|
|
|
|
if system_prompt:
|
|
|
|
messages.append({"role": "system", "content": system_prompt})
|
|
|
|
messages.extend(history_messages)
|
|
|
|
if prompt is not None:
|
|
|
|
messages.append({"role": "user", "content": prompt})
|
2024-12-06 10:21:53 +08:00
|
|
|
|
2024-10-21 20:40:49 +02:00
|
|
|
response = await openai_async_client.chat.completions.create(
|
|
|
|
model=model, messages=messages, **kwargs
|
|
|
|
)
|
2024-12-06 14:29:16 +08:00
|
|
|
content = response.choices[0].message.content
|
|
|
|
|
|
|
|
return content
|
2024-10-18 16:50:02 +01:00
|
|
|
|
2024-10-25 13:32:25 +05:30
|
|
|
|
2024-10-18 16:50:02 +01:00
|
|
|
class BedrockError(Exception):
|
|
|
|
"""Generic error for issues related to Amazon Bedrock"""
|
|
|
|
|
|
|
|
|
2024-10-18 14:17:14 +01:00
|
|
|
@retry(
|
2024-10-18 16:50:02 +01:00
|
|
|
stop=stop_after_attempt(5),
|
|
|
|
wait=wait_exponential(multiplier=1, max=60),
|
|
|
|
retry=retry_if_exception_type((BedrockError)),
|
2024-10-18 14:17:14 +01:00
|
|
|
)
|
|
|
|
async def bedrock_complete_if_cache(
|
2024-10-19 09:43:17 +05:30
|
|
|
model,
|
|
|
|
prompt,
|
|
|
|
system_prompt=None,
|
|
|
|
history_messages=[],
|
|
|
|
aws_access_key_id=None,
|
|
|
|
aws_secret_access_key=None,
|
|
|
|
aws_session_token=None,
|
|
|
|
**kwargs,
|
2024-10-18 14:17:14 +01:00
|
|
|
) -> str:
|
2024-10-19 09:43:17 +05:30
|
|
|
os.environ["AWS_ACCESS_KEY_ID"] = os.environ.get(
|
|
|
|
"AWS_ACCESS_KEY_ID", aws_access_key_id
|
|
|
|
)
|
|
|
|
os.environ["AWS_SECRET_ACCESS_KEY"] = os.environ.get(
|
|
|
|
"AWS_SECRET_ACCESS_KEY", aws_secret_access_key
|
|
|
|
)
|
|
|
|
os.environ["AWS_SESSION_TOKEN"] = os.environ.get(
|
|
|
|
"AWS_SESSION_TOKEN", aws_session_token
|
|
|
|
)
|
2024-12-08 17:35:52 +08:00
|
|
|
kwargs.pop("hashing_kv", None)
|
2024-10-18 16:50:02 +01:00
|
|
|
# Fix message history format
|
2024-10-18 14:17:14 +01:00
|
|
|
messages = []
|
2024-10-18 16:50:02 +01:00
|
|
|
for history_message in history_messages:
|
|
|
|
message = copy.copy(history_message)
|
2024-10-19 09:43:17 +05:30
|
|
|
message["content"] = [{"text": message["content"]}]
|
2024-10-18 16:50:02 +01:00
|
|
|
messages.append(message)
|
|
|
|
|
|
|
|
# Add user prompt
|
2024-10-19 09:43:17 +05:30
|
|
|
messages.append({"role": "user", "content": [{"text": prompt}]})
|
2024-12-06 14:29:16 +08:00
|
|
|
|
2024-10-18 16:50:02 +01:00
|
|
|
# Initialize Converse API arguments
|
2024-10-19 09:43:17 +05:30
|
|
|
args = {"modelId": model, "messages": messages}
|
2024-10-18 14:17:14 +01:00
|
|
|
|
2024-10-18 16:50:02 +01:00
|
|
|
# Define system prompt
|
2024-10-18 14:17:14 +01:00
|
|
|
if system_prompt:
|
2024-10-19 09:43:17 +05:30
|
|
|
args["system"] = [{"text": system_prompt}]
|
2024-10-18 14:17:14 +01:00
|
|
|
|
2024-10-18 16:50:02 +01:00
|
|
|
# Map and set up inference parameters
|
|
|
|
inference_params_map = {
|
2024-10-19 09:43:17 +05:30
|
|
|
"max_tokens": "maxTokens",
|
|
|
|
"top_p": "topP",
|
|
|
|
"stop_sequences": "stopSequences",
|
2024-10-18 16:50:02 +01:00
|
|
|
}
|
2024-10-19 09:43:17 +05:30
|
|
|
if inference_params := list(
|
|
|
|
set(kwargs) & set(["max_tokens", "temperature", "top_p", "stop_sequences"])
|
|
|
|
):
|
|
|
|
args["inferenceConfig"] = {}
|
2024-10-18 16:50:02 +01:00
|
|
|
for param in inference_params:
|
2024-10-19 09:43:17 +05:30
|
|
|
args["inferenceConfig"][inference_params_map.get(param, param)] = (
|
|
|
|
kwargs.pop(param)
|
|
|
|
)
|
2024-10-18 14:17:14 +01:00
|
|
|
|
2024-10-18 16:50:02 +01:00
|
|
|
# Call model via Converse API
|
2024-10-18 14:17:14 +01:00
|
|
|
session = aioboto3.Session()
|
|
|
|
async with session.client("bedrock-runtime") as bedrock_async_client:
|
2024-10-18 16:50:02 +01:00
|
|
|
try:
|
|
|
|
response = await bedrock_async_client.converse(**args, **kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
raise BedrockError(e)
|
2024-10-18 14:17:14 +01:00
|
|
|
|
2024-12-06 14:29:16 +08:00
|
|
|
return response["output"]["message"]["content"][0]["text"]
|
2024-10-18 14:17:14 +01:00
|
|
|
|
|
|
|
|
2024-10-23 15:02:28 +08:00
|
|
|
@lru_cache(maxsize=1)
|
|
|
|
def initialize_hf_model(model_name):
|
2024-10-25 13:32:25 +05:30
|
|
|
hf_tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
model_name, device_map="auto", trust_remote_code=True
|
|
|
|
)
|
|
|
|
hf_model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
model_name, device_map="auto", trust_remote_code=True
|
|
|
|
)
|
2024-10-23 15:25:46 +08:00
|
|
|
if hf_tokenizer.pad_token is None:
|
|
|
|
hf_tokenizer.pad_token = hf_tokenizer.eos_token
|
2024-10-23 15:02:28 +08:00
|
|
|
|
|
|
|
return hf_model, hf_tokenizer
|
|
|
|
|
|
|
|
|
2024-12-06 10:21:53 +08:00
|
|
|
@retry(
|
|
|
|
stop=stop_after_attempt(3),
|
|
|
|
wait=wait_exponential(multiplier=1, min=4, max=10),
|
|
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)),
|
|
|
|
)
|
2024-10-14 19:41:07 +08:00
|
|
|
async def hf_model_if_cache(
|
2024-12-06 08:17:20 +08:00
|
|
|
model,
|
|
|
|
prompt,
|
|
|
|
system_prompt=None,
|
|
|
|
history_messages=[],
|
|
|
|
**kwargs,
|
2024-10-14 19:41:07 +08:00
|
|
|
) -> str:
|
|
|
|
model_name = model
|
2024-10-23 15:02:28 +08:00
|
|
|
hf_model, hf_tokenizer = initialize_hf_model(model_name)
|
2024-10-14 19:41:07 +08:00
|
|
|
messages = []
|
|
|
|
if system_prompt:
|
|
|
|
messages.append({"role": "system", "content": system_prompt})
|
|
|
|
messages.extend(history_messages)
|
|
|
|
messages.append({"role": "user", "content": prompt})
|
2024-12-08 17:35:52 +08:00
|
|
|
kwargs.pop("hashing_kv", None)
|
2024-10-19 09:43:17 +05:30
|
|
|
input_prompt = ""
|
2024-10-14 19:41:07 +08:00
|
|
|
try:
|
2024-10-19 09:43:17 +05:30
|
|
|
input_prompt = hf_tokenizer.apply_chat_template(
|
|
|
|
messages, tokenize=False, add_generation_prompt=True
|
|
|
|
)
|
|
|
|
except Exception:
|
2024-10-14 19:41:07 +08:00
|
|
|
try:
|
|
|
|
ori_message = copy.deepcopy(messages)
|
2024-10-19 09:43:17 +05:30
|
|
|
if messages[0]["role"] == "system":
|
|
|
|
messages[1]["content"] = (
|
|
|
|
"<system>"
|
|
|
|
+ messages[0]["content"]
|
|
|
|
+ "</system>\n"
|
|
|
|
+ messages[1]["content"]
|
|
|
|
)
|
2024-10-14 19:41:07 +08:00
|
|
|
messages = messages[1:]
|
2024-10-19 09:43:17 +05:30
|
|
|
input_prompt = hf_tokenizer.apply_chat_template(
|
|
|
|
messages, tokenize=False, add_generation_prompt=True
|
|
|
|
)
|
|
|
|
except Exception:
|
2024-10-14 19:41:07 +08:00
|
|
|
len_message = len(ori_message)
|
|
|
|
for msgid in range(len_message):
|
2024-10-19 09:43:17 +05:30
|
|
|
input_prompt = (
|
|
|
|
input_prompt
|
|
|
|
+ "<"
|
|
|
|
+ ori_message[msgid]["role"]
|
|
|
|
+ ">"
|
|
|
|
+ ori_message[msgid]["content"]
|
|
|
|
+ "</"
|
|
|
|
+ ori_message[msgid]["role"]
|
|
|
|
+ ">\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
input_ids = hf_tokenizer(
|
|
|
|
input_prompt, return_tensors="pt", padding=True, truncation=True
|
|
|
|
).to("cuda")
|
2024-10-26 02:20:23 +08:00
|
|
|
inputs = {k: v.to(hf_model.device) for k, v in input_ids.items()}
|
2024-10-19 09:43:17 +05:30
|
|
|
output = hf_model.generate(
|
2024-10-26 02:42:40 +08:00
|
|
|
**input_ids, max_new_tokens=512, num_return_sequences=1, early_stopping=True
|
2024-10-19 09:43:17 +05:30
|
|
|
)
|
2024-10-26 16:24:35 +08:00
|
|
|
response_text = hf_tokenizer.decode(
|
|
|
|
output[0][len(inputs["input_ids"][0]) :], skip_special_tokens=True
|
2024-10-19 09:43:17 +05:30
|
|
|
)
|
2024-12-06 14:29:16 +08:00
|
|
|
|
2024-10-14 19:41:07 +08:00
|
|
|
return response_text
|
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
|
2024-12-06 10:21:53 +08:00
|
|
|
@retry(
|
|
|
|
stop=stop_after_attempt(3),
|
|
|
|
wait=wait_exponential(multiplier=1, min=4, max=10),
|
|
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)),
|
|
|
|
)
|
2024-10-16 15:15:10 +08:00
|
|
|
async def ollama_model_if_cache(
|
2024-12-06 08:17:20 +08:00
|
|
|
model,
|
|
|
|
prompt,
|
|
|
|
system_prompt=None,
|
|
|
|
history_messages=[],
|
|
|
|
**kwargs,
|
2024-12-06 08:48:55 +08:00
|
|
|
) -> Union[str, AsyncIterator[str]]:
|
|
|
|
stream = True if kwargs.get("stream") else False
|
2024-10-16 15:15:10 +08:00
|
|
|
kwargs.pop("max_tokens", None)
|
2024-11-29 21:41:37 +08:00
|
|
|
# kwargs.pop("response_format", None) # allow json
|
2024-10-21 11:53:06 +00:00
|
|
|
host = kwargs.pop("host", None)
|
|
|
|
timeout = kwargs.pop("timeout", None)
|
2024-12-08 17:35:52 +08:00
|
|
|
kwargs.pop("hashing_kv", None)
|
2024-10-21 11:53:06 +00:00
|
|
|
ollama_client = ollama.AsyncClient(host=host, timeout=timeout)
|
2024-10-16 15:15:10 +08:00
|
|
|
messages = []
|
|
|
|
if system_prompt:
|
|
|
|
messages.append({"role": "system", "content": system_prompt})
|
|
|
|
messages.extend(history_messages)
|
|
|
|
messages.append({"role": "user", "content": prompt})
|
2024-12-06 10:21:53 +08:00
|
|
|
|
2024-10-16 15:15:10 +08:00
|
|
|
response = await ollama_client.chat(model=model, messages=messages, **kwargs)
|
2024-12-06 08:48:55 +08:00
|
|
|
if stream:
|
2024-12-07 14:41:09 +08:00
|
|
|
"""cannot cache stream response"""
|
2024-10-16 15:15:10 +08:00
|
|
|
|
2024-12-06 08:48:55 +08:00
|
|
|
async def inner():
|
|
|
|
async for chunk in response:
|
|
|
|
yield chunk["message"]["content"]
|
2024-10-16 15:15:10 +08:00
|
|
|
|
2024-12-06 08:48:55 +08:00
|
|
|
return inner()
|
|
|
|
else:
|
2024-12-08 17:38:49 +08:00
|
|
|
return response["message"]["content"]
|
2024-10-14 19:41:07 +08:00
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
|
2024-10-26 16:11:15 +08:00
|
|
|
@lru_cache(maxsize=1)
|
2024-10-26 16:24:35 +08:00
|
|
|
def initialize_lmdeploy_pipeline(
|
|
|
|
model,
|
|
|
|
tp=1,
|
|
|
|
chat_template=None,
|
|
|
|
log_level="WARNING",
|
|
|
|
model_format="hf",
|
|
|
|
quant_policy=0,
|
|
|
|
):
|
2024-10-26 16:11:15 +08:00
|
|
|
from lmdeploy import pipeline, ChatTemplateConfig, TurbomindEngineConfig
|
2024-10-26 16:24:35 +08:00
|
|
|
|
2024-10-26 16:11:15 +08:00
|
|
|
lmdeploy_pipe = pipeline(
|
|
|
|
model_path=model,
|
2024-10-26 16:24:35 +08:00
|
|
|
backend_config=TurbomindEngineConfig(
|
|
|
|
tp=tp, model_format=model_format, quant_policy=quant_policy
|
|
|
|
),
|
2024-11-29 21:41:37 +08:00
|
|
|
chat_template_config=(
|
|
|
|
ChatTemplateConfig(model_name=chat_template) if chat_template else None
|
|
|
|
),
|
2024-10-26 16:24:35 +08:00
|
|
|
log_level="WARNING",
|
|
|
|
)
|
2024-10-26 16:11:15 +08:00
|
|
|
return lmdeploy_pipe
|
|
|
|
|
|
|
|
|
2024-12-06 10:21:53 +08:00
|
|
|
@retry(
|
|
|
|
stop=stop_after_attempt(3),
|
|
|
|
wait=wait_exponential(multiplier=1, min=4, max=10),
|
|
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)),
|
|
|
|
)
|
2024-10-26 16:11:15 +08:00
|
|
|
async def lmdeploy_model_if_cache(
|
2024-10-26 16:24:35 +08:00
|
|
|
model,
|
|
|
|
prompt,
|
|
|
|
system_prompt=None,
|
|
|
|
history_messages=[],
|
|
|
|
chat_template=None,
|
|
|
|
model_format="hf",
|
|
|
|
quant_policy=0,
|
|
|
|
**kwargs,
|
2024-10-26 16:11:15 +08:00
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
model (str): The path to the model.
|
|
|
|
It could be one of the following options:
|
|
|
|
- i) A local directory path of a turbomind model which is
|
|
|
|
converted by `lmdeploy convert` command or download
|
|
|
|
from ii) and iii).
|
|
|
|
- ii) The model_id of a lmdeploy-quantized model hosted
|
|
|
|
inside a model repo on huggingface.co, such as
|
|
|
|
"InternLM/internlm-chat-20b-4bit",
|
|
|
|
"lmdeploy/llama2-chat-70b-4bit", etc.
|
|
|
|
- iii) The model_id of a model hosted inside a model repo
|
|
|
|
on huggingface.co, such as "internlm/internlm-chat-7b",
|
|
|
|
"Qwen/Qwen-7B-Chat ", "baichuan-inc/Baichuan2-7B-Chat"
|
|
|
|
and so on.
|
|
|
|
chat_template (str): needed when model is a pytorch model on
|
|
|
|
huggingface.co, such as "internlm-chat-7b",
|
2024-10-26 16:24:35 +08:00
|
|
|
"Qwen-7B-Chat ", "Baichuan2-7B-Chat" and so on,
|
2024-10-26 16:11:15 +08:00
|
|
|
and when the model name of local path did not match the original model name in HF.
|
|
|
|
tp (int): tensor parallel
|
|
|
|
prompt (Union[str, List[str]]): input texts to be completed.
|
|
|
|
do_preprocess (bool): whether pre-process the messages. Default to
|
|
|
|
True, which means chat_template will be applied.
|
|
|
|
skip_special_tokens (bool): Whether or not to remove special tokens
|
2024-10-26 16:24:35 +08:00
|
|
|
in the decoding. Default to be True.
|
|
|
|
do_sample (bool): Whether or not to use sampling, use greedy decoding otherwise.
|
2024-10-26 16:11:15 +08:00
|
|
|
Default to be False, which means greedy decoding will be applied.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
import lmdeploy
|
|
|
|
from lmdeploy import version_info, GenerationConfig
|
2024-10-26 16:24:35 +08:00
|
|
|
except Exception:
|
2024-12-09 22:08:06 +09:00
|
|
|
raise ImportError("Please install lmdeploy before initialize lmdeploy backend.")
|
2024-12-08 17:35:52 +08:00
|
|
|
kwargs.pop("hashing_kv", None)
|
2024-10-26 16:11:15 +08:00
|
|
|
kwargs.pop("response_format", None)
|
|
|
|
max_new_tokens = kwargs.pop("max_tokens", 512)
|
2024-10-26 16:24:35 +08:00
|
|
|
tp = kwargs.pop("tp", 1)
|
|
|
|
skip_special_tokens = kwargs.pop("skip_special_tokens", True)
|
|
|
|
do_preprocess = kwargs.pop("do_preprocess", True)
|
|
|
|
do_sample = kwargs.pop("do_sample", False)
|
2024-10-26 16:11:15 +08:00
|
|
|
gen_params = kwargs
|
2024-10-26 16:24:35 +08:00
|
|
|
|
2024-10-26 16:11:15 +08:00
|
|
|
version = version_info
|
|
|
|
if do_sample is not None and version < (0, 6, 0):
|
|
|
|
raise RuntimeError(
|
2024-10-26 16:24:35 +08:00
|
|
|
"`do_sample` parameter is not supported by lmdeploy until "
|
|
|
|
f"v0.6.0, but currently using lmdeloy {lmdeploy.__version__}"
|
|
|
|
)
|
2024-10-26 16:11:15 +08:00
|
|
|
else:
|
|
|
|
do_sample = True
|
|
|
|
gen_params.update(do_sample=do_sample)
|
|
|
|
|
|
|
|
lmdeploy_pipe = initialize_lmdeploy_pipeline(
|
|
|
|
model=model,
|
|
|
|
tp=tp,
|
|
|
|
chat_template=chat_template,
|
|
|
|
model_format=model_format,
|
|
|
|
quant_policy=quant_policy,
|
2024-10-26 16:24:35 +08:00
|
|
|
log_level="WARNING",
|
|
|
|
)
|
2024-10-26 16:11:15 +08:00
|
|
|
|
|
|
|
messages = []
|
|
|
|
if system_prompt:
|
|
|
|
messages.append({"role": "system", "content": system_prompt})
|
|
|
|
|
|
|
|
messages.extend(history_messages)
|
|
|
|
messages.append({"role": "user", "content": prompt})
|
2024-12-06 10:21:53 +08:00
|
|
|
|
2024-10-26 16:11:15 +08:00
|
|
|
gen_config = GenerationConfig(
|
2024-10-26 16:24:35 +08:00
|
|
|
skip_special_tokens=skip_special_tokens,
|
|
|
|
max_new_tokens=max_new_tokens,
|
|
|
|
**gen_params,
|
|
|
|
)
|
2024-10-26 16:11:15 +08:00
|
|
|
|
|
|
|
response = ""
|
2024-10-26 16:24:35 +08:00
|
|
|
async for res in lmdeploy_pipe.generate(
|
|
|
|
messages,
|
|
|
|
gen_config=gen_config,
|
|
|
|
do_preprocess=do_preprocess,
|
|
|
|
stream_response=False,
|
|
|
|
session_id=1,
|
|
|
|
):
|
2024-10-26 16:11:15 +08:00
|
|
|
response += res.response
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2024-11-29 21:41:37 +08:00
|
|
|
class GPTKeywordExtractionFormat(BaseModel):
|
|
|
|
high_level_keywords: List[str]
|
|
|
|
low_level_keywords: List[str]
|
|
|
|
|
|
|
|
|
2024-12-07 14:41:09 +08:00
|
|
|
async def openai_complete(
|
|
|
|
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
|
|
|
) -> Union[str, AsyncIterator[str]]:
|
|
|
|
keyword_extraction = kwargs.pop("keyword_extraction", None)
|
|
|
|
if keyword_extraction:
|
|
|
|
kwargs["response_format"] = "json"
|
|
|
|
model_name = kwargs["hashing_kv"].global_config["llm_model_name"]
|
|
|
|
return await openai_complete_if_cache(
|
|
|
|
model_name,
|
|
|
|
prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-10-10 15:02:30 +08:00
|
|
|
async def gpt_4o_complete(
|
2024-11-29 21:41:37 +08:00
|
|
|
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
2024-10-10 15:02:30 +08:00
|
|
|
) -> str:
|
2024-12-05 11:47:56 +08:00
|
|
|
keyword_extraction = kwargs.pop("keyword_extraction", None)
|
2024-11-29 21:41:37 +08:00
|
|
|
if keyword_extraction:
|
|
|
|
kwargs["response_format"] = GPTKeywordExtractionFormat
|
2024-10-10 15:02:30 +08:00
|
|
|
return await openai_complete_if_cache(
|
|
|
|
"gpt-4o",
|
|
|
|
prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def gpt_4o_mini_complete(
|
2024-11-29 21:41:37 +08:00
|
|
|
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
2024-10-10 15:02:30 +08:00
|
|
|
) -> str:
|
2024-12-05 11:47:56 +08:00
|
|
|
keyword_extraction = kwargs.pop("keyword_extraction", None)
|
2024-11-29 21:41:37 +08:00
|
|
|
if keyword_extraction:
|
|
|
|
kwargs["response_format"] = GPTKeywordExtractionFormat
|
2024-10-10 15:02:30 +08:00
|
|
|
return await openai_complete_if_cache(
|
|
|
|
"gpt-4o-mini",
|
|
|
|
prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
2024-12-04 19:44:04 +08:00
|
|
|
|
2024-12-03 17:15:10 +07:00
|
|
|
async def nvidia_openai_complete(
|
|
|
|
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
|
|
|
) -> str:
|
2024-12-05 11:47:56 +08:00
|
|
|
keyword_extraction = kwargs.pop("keyword_extraction", None)
|
2024-12-03 17:15:10 +07:00
|
|
|
result = await openai_complete_if_cache(
|
2024-12-04 19:44:04 +08:00
|
|
|
"nvidia/llama-3.1-nemotron-70b-instruct", # context length 128k
|
2024-12-03 17:15:10 +07:00
|
|
|
prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
base_url="https://integrate.api.nvidia.com/v1",
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
if keyword_extraction: # TODO: use JSON API
|
|
|
|
return locate_json_string_body_from_string(result)
|
|
|
|
return result
|
2024-10-18 14:17:14 +01:00
|
|
|
|
2024-12-04 19:44:04 +08:00
|
|
|
|
2024-10-21 20:40:49 +02:00
|
|
|
async def azure_openai_complete(
|
2024-11-29 21:41:37 +08:00
|
|
|
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
2024-10-21 20:40:49 +02:00
|
|
|
) -> str:
|
2024-12-05 11:47:56 +08:00
|
|
|
keyword_extraction = kwargs.pop("keyword_extraction", None)
|
2024-11-29 21:41:37 +08:00
|
|
|
result = await azure_openai_complete_if_cache(
|
2024-10-21 20:40:49 +02:00
|
|
|
"conversation-4o-mini",
|
|
|
|
prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
**kwargs,
|
|
|
|
)
|
2024-11-29 21:41:37 +08:00
|
|
|
if keyword_extraction: # TODO: use JSON API
|
|
|
|
return locate_json_string_body_from_string(result)
|
|
|
|
return result
|
2024-10-18 14:17:14 +01:00
|
|
|
|
2024-10-25 13:32:25 +05:30
|
|
|
|
2024-10-18 14:17:14 +01:00
|
|
|
async def bedrock_complete(
|
2024-11-29 21:41:37 +08:00
|
|
|
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
2024-10-18 14:17:14 +01:00
|
|
|
) -> str:
|
2024-12-05 11:47:56 +08:00
|
|
|
keyword_extraction = kwargs.pop("keyword_extraction", None)
|
2024-11-29 21:41:37 +08:00
|
|
|
result = await bedrock_complete_if_cache(
|
2024-10-18 16:50:02 +01:00
|
|
|
"anthropic.claude-3-haiku-20240307-v1:0",
|
2024-10-18 14:17:14 +01:00
|
|
|
prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
**kwargs,
|
|
|
|
)
|
2024-11-29 21:41:37 +08:00
|
|
|
if keyword_extraction: # TODO: use JSON API
|
|
|
|
return locate_json_string_body_from_string(result)
|
|
|
|
return result
|
2024-10-18 14:17:14 +01:00
|
|
|
|
|
|
|
|
2024-10-14 20:33:46 +08:00
|
|
|
async def hf_model_complete(
|
2024-11-29 21:41:37 +08:00
|
|
|
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
2024-10-14 19:41:07 +08:00
|
|
|
) -> str:
|
2024-12-05 11:47:56 +08:00
|
|
|
keyword_extraction = kwargs.pop("keyword_extraction", None)
|
2024-10-19 09:43:17 +05:30
|
|
|
model_name = kwargs["hashing_kv"].global_config["llm_model_name"]
|
2024-11-29 21:41:37 +08:00
|
|
|
result = await hf_model_if_cache(
|
2024-10-15 20:06:59 +08:00
|
|
|
model_name,
|
2024-10-14 19:41:07 +08:00
|
|
|
prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
**kwargs,
|
|
|
|
)
|
2024-11-29 21:41:37 +08:00
|
|
|
if keyword_extraction: # TODO: use JSON API
|
|
|
|
return locate_json_string_body_from_string(result)
|
|
|
|
return result
|
2024-10-14 19:41:07 +08:00
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
|
2024-10-16 15:15:10 +08:00
|
|
|
async def ollama_model_complete(
|
2024-11-29 21:41:37 +08:00
|
|
|
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
2024-12-06 08:48:55 +08:00
|
|
|
) -> Union[str, AsyncIterator[str]]:
|
2024-12-05 11:47:56 +08:00
|
|
|
keyword_extraction = kwargs.pop("keyword_extraction", None)
|
2024-11-29 21:41:37 +08:00
|
|
|
if keyword_extraction:
|
2024-11-30 00:00:51 +08:00
|
|
|
kwargs["format"] = "json"
|
2024-10-19 09:43:17 +05:30
|
|
|
model_name = kwargs["hashing_kv"].global_config["llm_model_name"]
|
2024-10-16 15:15:10 +08:00
|
|
|
return await ollama_model_if_cache(
|
|
|
|
model_name,
|
|
|
|
prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
|
2024-12-13 16:18:33 +08:00
|
|
|
@retry(
|
|
|
|
stop=stop_after_attempt(3),
|
|
|
|
wait=wait_exponential(multiplier=1, min=4, max=10),
|
|
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)),
|
|
|
|
)
|
|
|
|
async def zhipu_complete_if_cache(
|
|
|
|
prompt: Union[str, List[Dict[str, str]]],
|
|
|
|
model: str = "glm-4-flashx", # The most cost/performance balance model in glm-4 series
|
|
|
|
api_key: Optional[str] = None,
|
|
|
|
system_prompt: Optional[str] = None,
|
|
|
|
history_messages: List[Dict[str, str]] = [],
|
|
|
|
**kwargs
|
|
|
|
) -> str:
|
|
|
|
# dynamically load ZhipuAI
|
|
|
|
try:
|
|
|
|
from zhipuai import ZhipuAI
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError("Please install zhipuai before initialize zhipuai backend.")
|
|
|
|
|
|
|
|
if api_key:
|
|
|
|
client = ZhipuAI(api_key=api_key)
|
|
|
|
else:
|
|
|
|
# please set ZHIPUAI_API_KEY in your environment
|
|
|
|
# os.environ["ZHIPUAI_API_KEY"]
|
|
|
|
client = ZhipuAI()
|
|
|
|
|
|
|
|
messages = []
|
|
|
|
|
|
|
|
if not system_prompt:
|
|
|
|
system_prompt = "You are a helpful assistant. 注意内容里的敏感词用***替换。"
|
|
|
|
|
|
|
|
# Add system prompt if provided
|
|
|
|
if system_prompt:
|
|
|
|
messages.append({"role": "system", "content": system_prompt})
|
|
|
|
messages.extend(history_messages)
|
|
|
|
messages.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
|
# Add debug logging
|
|
|
|
logger.debug("===== Query Input to LLM =====")
|
|
|
|
logger.debug(f"Query: {prompt}")
|
|
|
|
logger.debug(f"System prompt: {system_prompt}")
|
|
|
|
|
|
|
|
# Remove unsupported kwargs
|
|
|
|
kwargs = {k: v for k, v in kwargs.items() if k not in ['hashing_kv', 'keyword_extraction']}
|
|
|
|
|
|
|
|
response = client.chat.completions.create(
|
|
|
|
model=model,
|
|
|
|
messages=messages,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
async def zhipu_complete(
|
|
|
|
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
|
|
|
):
|
|
|
|
# Pop keyword_extraction from kwargs to avoid passing it to zhipu_complete_if_cache
|
|
|
|
keyword_extraction = kwargs.pop("keyword_extraction", None)
|
|
|
|
|
|
|
|
if keyword_extraction:
|
|
|
|
# Add a system prompt to guide the model to return JSON format
|
|
|
|
extraction_prompt = """You are a helpful assistant that extracts keywords from text.
|
|
|
|
Please analyze the content and extract two types of keywords:
|
|
|
|
1. High-level keywords: Important concepts and main themes
|
|
|
|
2. Low-level keywords: Specific details and supporting elements
|
|
|
|
|
|
|
|
Return your response in this exact JSON format:
|
|
|
|
{
|
|
|
|
"high_level_keywords": ["keyword1", "keyword2"],
|
|
|
|
"low_level_keywords": ["keyword1", "keyword2", "keyword3"]
|
|
|
|
}
|
|
|
|
|
|
|
|
Only return the JSON, no other text."""
|
|
|
|
|
|
|
|
# Combine with existing system prompt if any
|
|
|
|
if system_prompt:
|
|
|
|
system_prompt = f"{system_prompt}\n\n{extraction_prompt}"
|
|
|
|
else:
|
|
|
|
system_prompt = extraction_prompt
|
|
|
|
|
|
|
|
try:
|
|
|
|
response = await zhipu_complete_if_cache(
|
|
|
|
prompt=prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
# Try to parse as JSON
|
|
|
|
try:
|
|
|
|
data = json.loads(response)
|
|
|
|
return GPTKeywordExtractionFormat(
|
|
|
|
high_level_keywords=data.get("high_level_keywords", []),
|
|
|
|
low_level_keywords=data.get("low_level_keywords", [])
|
|
|
|
)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
# If direct JSON parsing fails, try to extract JSON from text
|
|
|
|
match = re.search(r"\{[\s\S]*\}", response)
|
|
|
|
if match:
|
|
|
|
try:
|
|
|
|
data = json.loads(match.group())
|
|
|
|
return GPTKeywordExtractionFormat(
|
|
|
|
high_level_keywords=data.get("high_level_keywords", []),
|
|
|
|
low_level_keywords=data.get("low_level_keywords", [])
|
|
|
|
)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# If all parsing fails, log warning and return empty format
|
|
|
|
logger.warning(f"Failed to parse keyword extraction response: {response}")
|
|
|
|
return GPTKeywordExtractionFormat(
|
|
|
|
high_level_keywords=[], low_level_keywords=[]
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error during keyword extraction: {str(e)}")
|
|
|
|
return GPTKeywordExtractionFormat(
|
|
|
|
high_level_keywords=[], low_level_keywords=[]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# For non-keyword-extraction, just return the raw response string
|
|
|
|
return await zhipu_complete_if_cache(
|
|
|
|
prompt=prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@wrap_embedding_func_with_attrs(embedding_dim=1024, max_token_size=8192)
|
|
|
|
@retry(
|
|
|
|
stop=stop_after_attempt(3),
|
|
|
|
wait=wait_exponential(multiplier=1, min=4, max=60),
|
|
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)),
|
|
|
|
)
|
|
|
|
async def zhipu_embedding(
|
|
|
|
texts: list[str],
|
|
|
|
model: str = "embedding-3",
|
|
|
|
api_key: str = None,
|
|
|
|
**kwargs
|
|
|
|
) -> np.ndarray:
|
|
|
|
|
|
|
|
# dynamically load ZhipuAI
|
|
|
|
try:
|
|
|
|
from zhipuai import ZhipuAI
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError("Please install zhipuai before initialize zhipuai backend.")
|
|
|
|
if api_key:
|
|
|
|
client = ZhipuAI(api_key=api_key)
|
|
|
|
else:
|
|
|
|
# please set ZHIPUAI_API_KEY in your environment
|
|
|
|
# os.environ["ZHIPUAI_API_KEY"]
|
|
|
|
client = ZhipuAI()
|
|
|
|
|
|
|
|
# Convert single text to list if needed
|
|
|
|
if isinstance(texts, str):
|
|
|
|
texts = [texts]
|
|
|
|
|
|
|
|
embeddings = []
|
|
|
|
for text in texts:
|
|
|
|
try:
|
|
|
|
response = client.embeddings.create(
|
|
|
|
model=model,
|
|
|
|
input=[text],
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
embeddings.append(response.data[0].embedding)
|
|
|
|
except Exception as e:
|
|
|
|
raise Exception(f"Error calling ChatGLM Embedding API: {str(e)}")
|
|
|
|
|
|
|
|
return np.array(embeddings)
|
|
|
|
|
|
|
|
|
2024-10-10 15:02:30 +08:00
|
|
|
@wrap_embedding_func_with_attrs(embedding_dim=1536, max_token_size=8192)
|
|
|
|
@retry(
|
|
|
|
stop=stop_after_attempt(3),
|
2024-10-22 15:16:57 +08:00
|
|
|
wait=wait_exponential(multiplier=1, min=4, max=60),
|
2024-10-10 15:02:30 +08:00
|
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)),
|
|
|
|
)
|
2024-10-19 09:43:17 +05:30
|
|
|
async def openai_embedding(
|
|
|
|
texts: list[str],
|
|
|
|
model: str = "text-embedding-3-small",
|
|
|
|
base_url: str = None,
|
2024-11-25 13:40:38 +08:00
|
|
|
api_key: str = None,
|
2024-10-19 09:43:17 +05:30
|
|
|
) -> np.ndarray:
|
2024-10-15 12:55:05 -07:00
|
|
|
if api_key:
|
|
|
|
os.environ["OPENAI_API_KEY"] = api_key
|
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
openai_async_client = (
|
|
|
|
AsyncOpenAI() if base_url is None else AsyncOpenAI(base_url=base_url)
|
|
|
|
)
|
2024-10-10 15:02:30 +08:00
|
|
|
response = await openai_async_client.embeddings.create(
|
2024-11-25 13:40:38 +08:00
|
|
|
model=model, input=texts, encoding_format="float"
|
2024-10-10 15:02:30 +08:00
|
|
|
)
|
|
|
|
return np.array([dp.embedding for dp in response.data])
|
|
|
|
|
2024-10-14 19:41:07 +08:00
|
|
|
|
2024-12-08 22:20:41 +08:00
|
|
|
async def fetch_data(url, headers, data):
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.post(url, headers=headers, json=data) as response:
|
|
|
|
response_json = await response.json()
|
|
|
|
data_list = response_json.get("data", [])
|
|
|
|
return data_list
|
|
|
|
|
|
|
|
|
|
|
|
async def jina_embedding(
|
|
|
|
texts: list[str],
|
|
|
|
dimensions: int = 1024,
|
|
|
|
late_chunking: bool = False,
|
|
|
|
base_url: str = None,
|
|
|
|
api_key: str = None,
|
|
|
|
) -> np.ndarray:
|
|
|
|
if api_key:
|
|
|
|
os.environ["JINA_API_KEY"] = api_key
|
|
|
|
url = "https://api.jina.ai/v1/embeddings" if not base_url else base_url
|
|
|
|
headers = {
|
|
|
|
"Content-Type": "application/json",
|
2024-12-09 17:05:17 +08:00
|
|
|
"Authorization": f"Bearer {os.environ['JINA_API_KEY']}",
|
2024-12-08 22:20:41 +08:00
|
|
|
}
|
|
|
|
data = {
|
|
|
|
"model": "jina-embeddings-v3",
|
|
|
|
"normalized": True,
|
|
|
|
"embedding_type": "float",
|
|
|
|
"dimensions": f"{dimensions}",
|
|
|
|
"late_chunking": late_chunking,
|
|
|
|
"input": texts,
|
|
|
|
}
|
|
|
|
data_list = await fetch_data(url, headers, data)
|
|
|
|
return np.array([dp["embedding"] for dp in data_list])
|
|
|
|
|
|
|
|
|
2024-12-03 17:15:10 +07:00
|
|
|
@wrap_embedding_func_with_attrs(embedding_dim=2048, max_token_size=512)
|
|
|
|
@retry(
|
|
|
|
stop=stop_after_attempt(3),
|
|
|
|
wait=wait_exponential(multiplier=1, min=4, max=60),
|
|
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)),
|
|
|
|
)
|
|
|
|
async def nvidia_openai_embedding(
|
|
|
|
texts: list[str],
|
2024-12-06 10:28:35 +08:00
|
|
|
model: str = "nvidia/llama-3.2-nv-embedqa-1b-v1",
|
|
|
|
# refer to https://build.nvidia.com/nim?filters=usecase%3Ausecase_text_to_embedding
|
2024-12-03 17:15:10 +07:00
|
|
|
base_url: str = "https://integrate.api.nvidia.com/v1",
|
|
|
|
api_key: str = None,
|
2024-12-04 19:44:04 +08:00
|
|
|
input_type: str = "passage", # query for retrieval, passage for embedding
|
|
|
|
trunc: str = "NONE", # NONE or START or END
|
|
|
|
encode: str = "float", # float or base64
|
2024-12-03 17:15:10 +07:00
|
|
|
) -> np.ndarray:
|
|
|
|
if api_key:
|
|
|
|
os.environ["OPENAI_API_KEY"] = api_key
|
|
|
|
|
|
|
|
openai_async_client = (
|
|
|
|
AsyncOpenAI() if base_url is None else AsyncOpenAI(base_url=base_url)
|
|
|
|
)
|
|
|
|
response = await openai_async_client.embeddings.create(
|
2024-12-04 19:44:04 +08:00
|
|
|
model=model,
|
|
|
|
input=texts,
|
|
|
|
encoding_format=encode,
|
|
|
|
extra_body={"input_type": input_type, "truncate": trunc},
|
2024-12-03 17:15:10 +07:00
|
|
|
)
|
|
|
|
return np.array([dp.embedding for dp in response.data])
|
|
|
|
|
2024-12-04 19:44:04 +08:00
|
|
|
|
2024-11-30 17:16:07 +01:00
|
|
|
@wrap_embedding_func_with_attrs(embedding_dim=1536, max_token_size=8191)
|
2024-10-21 20:40:49 +02:00
|
|
|
@retry(
|
|
|
|
stop=stop_after_attempt(3),
|
|
|
|
wait=wait_exponential(multiplier=1, min=4, max=10),
|
|
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)),
|
|
|
|
)
|
|
|
|
async def azure_openai_embedding(
|
|
|
|
texts: list[str],
|
|
|
|
model: str = "text-embedding-3-small",
|
|
|
|
base_url: str = None,
|
|
|
|
api_key: str = None,
|
2024-11-30 17:11:38 +01:00
|
|
|
api_version: str = None,
|
2024-10-21 20:40:49 +02:00
|
|
|
) -> np.ndarray:
|
|
|
|
if api_key:
|
|
|
|
os.environ["AZURE_OPENAI_API_KEY"] = api_key
|
|
|
|
if base_url:
|
|
|
|
os.environ["AZURE_OPENAI_ENDPOINT"] = base_url
|
2024-11-30 17:11:38 +01:00
|
|
|
if api_version:
|
|
|
|
os.environ["AZURE_OPENAI_API_VERSION"] = api_version
|
2024-10-21 20:40:49 +02:00
|
|
|
|
2024-10-25 13:32:25 +05:30
|
|
|
openai_async_client = AsyncAzureOpenAI(
|
|
|
|
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
|
|
|
|
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
|
|
|
|
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
|
|
|
|
)
|
2024-10-21 20:40:49 +02:00
|
|
|
|
|
|
|
response = await openai_async_client.embeddings.create(
|
|
|
|
model=model, input=texts, encoding_format="float"
|
|
|
|
)
|
|
|
|
return np.array([dp.embedding for dp in response.data])
|
|
|
|
|
2024-10-14 19:41:07 +08:00
|
|
|
|
2024-10-22 15:16:57 +08:00
|
|
|
@retry(
|
|
|
|
stop=stop_after_attempt(3),
|
|
|
|
wait=wait_exponential(multiplier=1, min=4, max=60),
|
|
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)),
|
|
|
|
)
|
|
|
|
async def siliconcloud_embedding(
|
|
|
|
texts: list[str],
|
|
|
|
model: str = "netease-youdao/bce-embedding-base_v1",
|
|
|
|
base_url: str = "https://api.siliconflow.cn/v1/embeddings",
|
|
|
|
max_token_size: int = 512,
|
|
|
|
api_key: str = None,
|
|
|
|
) -> np.ndarray:
|
2024-10-25 13:32:25 +05:30
|
|
|
if api_key and not api_key.startswith("Bearer "):
|
|
|
|
api_key = "Bearer " + api_key
|
2024-10-22 15:16:57 +08:00
|
|
|
|
2024-10-25 13:32:25 +05:30
|
|
|
headers = {"Authorization": api_key, "Content-Type": "application/json"}
|
2024-10-22 15:16:57 +08:00
|
|
|
|
|
|
|
truncate_texts = [text[0:max_token_size] for text in texts]
|
|
|
|
|
2024-10-25 13:32:25 +05:30
|
|
|
payload = {"model": model, "input": truncate_texts, "encoding_format": "base64"}
|
2024-10-22 15:16:57 +08:00
|
|
|
|
|
|
|
base64_strings = []
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.post(base_url, headers=headers, json=payload) as response:
|
|
|
|
content = await response.json()
|
2024-10-25 13:32:25 +05:30
|
|
|
if "code" in content:
|
2024-10-22 15:16:57 +08:00
|
|
|
raise ValueError(content)
|
2024-10-25 13:32:25 +05:30
|
|
|
base64_strings = [item["embedding"] for item in content["data"]]
|
|
|
|
|
2024-10-22 15:16:57 +08:00
|
|
|
embeddings = []
|
|
|
|
for string in base64_strings:
|
|
|
|
decode_bytes = base64.b64decode(string)
|
|
|
|
n = len(decode_bytes) // 4
|
2024-10-25 13:32:25 +05:30
|
|
|
float_array = struct.unpack("<" + "f" * n, decode_bytes)
|
2024-10-22 15:16:57 +08:00
|
|
|
embeddings.append(float_array)
|
|
|
|
return np.array(embeddings)
|
2024-10-14 19:41:07 +08:00
|
|
|
|
2024-10-23 11:08:40 +08:00
|
|
|
|
2024-10-18 14:17:14 +01:00
|
|
|
# @wrap_embedding_func_with_attrs(embedding_dim=1024, max_token_size=8192)
|
|
|
|
# @retry(
|
|
|
|
# stop=stop_after_attempt(3),
|
|
|
|
# wait=wait_exponential(multiplier=1, min=4, max=10),
|
|
|
|
# retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)), # TODO: fix exceptions
|
|
|
|
# )
|
|
|
|
async def bedrock_embedding(
|
2024-10-19 09:43:17 +05:30
|
|
|
texts: list[str],
|
|
|
|
model: str = "amazon.titan-embed-text-v2:0",
|
|
|
|
aws_access_key_id=None,
|
|
|
|
aws_secret_access_key=None,
|
|
|
|
aws_session_token=None,
|
|
|
|
) -> np.ndarray:
|
|
|
|
os.environ["AWS_ACCESS_KEY_ID"] = os.environ.get(
|
|
|
|
"AWS_ACCESS_KEY_ID", aws_access_key_id
|
|
|
|
)
|
|
|
|
os.environ["AWS_SECRET_ACCESS_KEY"] = os.environ.get(
|
|
|
|
"AWS_SECRET_ACCESS_KEY", aws_secret_access_key
|
|
|
|
)
|
|
|
|
os.environ["AWS_SESSION_TOKEN"] = os.environ.get(
|
|
|
|
"AWS_SESSION_TOKEN", aws_session_token
|
|
|
|
)
|
2024-10-18 14:17:14 +01:00
|
|
|
|
|
|
|
session = aioboto3.Session()
|
|
|
|
async with session.client("bedrock-runtime") as bedrock_async_client:
|
|
|
|
if (model_provider := model.split(".")[0]) == "amazon":
|
|
|
|
embed_texts = []
|
|
|
|
for text in texts:
|
|
|
|
if "v2" in model:
|
2024-10-19 09:43:17 +05:30
|
|
|
body = json.dumps(
|
|
|
|
{
|
|
|
|
"inputText": text,
|
|
|
|
# 'dimensions': embedding_dim,
|
|
|
|
"embeddingTypes": ["float"],
|
|
|
|
}
|
|
|
|
)
|
2024-10-18 14:17:14 +01:00
|
|
|
elif "v1" in model:
|
2024-10-19 09:43:17 +05:30
|
|
|
body = json.dumps({"inputText": text})
|
2024-10-18 14:17:14 +01:00
|
|
|
else:
|
|
|
|
raise ValueError(f"Model {model} is not supported!")
|
|
|
|
|
|
|
|
response = await bedrock_async_client.invoke_model(
|
|
|
|
modelId=model,
|
|
|
|
body=body,
|
|
|
|
accept="application/json",
|
2024-10-19 09:43:17 +05:30
|
|
|
contentType="application/json",
|
2024-10-18 14:17:14 +01:00
|
|
|
)
|
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
response_body = await response.get("body").json()
|
2024-10-18 14:17:14 +01:00
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
embed_texts.append(response_body["embedding"])
|
2024-10-18 14:17:14 +01:00
|
|
|
elif model_provider == "cohere":
|
2024-10-19 09:43:17 +05:30
|
|
|
body = json.dumps(
|
|
|
|
{"texts": texts, "input_type": "search_document", "truncate": "NONE"}
|
|
|
|
)
|
2024-10-18 14:17:14 +01:00
|
|
|
|
|
|
|
response = await bedrock_async_client.invoke_model(
|
|
|
|
model=model,
|
|
|
|
body=body,
|
|
|
|
accept="application/json",
|
2024-10-19 09:43:17 +05:30
|
|
|
contentType="application/json",
|
2024-10-18 14:17:14 +01:00
|
|
|
)
|
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
response_body = json.loads(response.get("body").read())
|
2024-10-18 14:17:14 +01:00
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
embed_texts = response_body["embeddings"]
|
2024-10-18 14:17:14 +01:00
|
|
|
else:
|
|
|
|
raise ValueError(f"Model provider '{model_provider}' is not supported!")
|
|
|
|
|
|
|
|
return np.array(embed_texts)
|
|
|
|
|
|
|
|
|
2024-10-15 19:40:08 +08:00
|
|
|
async def hf_embedding(texts: list[str], tokenizer, embed_model) -> np.ndarray:
|
2024-11-13 14:20:36 +08:00
|
|
|
device = next(embed_model.parameters()).device
|
2024-10-19 09:43:17 +05:30
|
|
|
input_ids = tokenizer(
|
|
|
|
texts, return_tensors="pt", padding=True, truncation=True
|
2024-11-13 14:20:36 +08:00
|
|
|
).input_ids.to(device)
|
2024-10-14 19:41:07 +08:00
|
|
|
with torch.no_grad():
|
2024-10-15 19:40:08 +08:00
|
|
|
outputs = embed_model(input_ids)
|
2024-10-14 19:41:07 +08:00
|
|
|
embeddings = outputs.last_hidden_state.mean(dim=1)
|
2024-11-13 14:20:36 +08:00
|
|
|
if embeddings.dtype == torch.bfloat16:
|
|
|
|
return embeddings.detach().to(torch.float32).cpu().numpy()
|
|
|
|
else:
|
|
|
|
return embeddings.detach().cpu().numpy()
|
2024-10-14 19:41:07 +08:00
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
|
2024-10-21 11:53:06 +00:00
|
|
|
async def ollama_embedding(texts: list[str], embed_model, **kwargs) -> np.ndarray:
|
2024-12-03 08:41:12 +08:00
|
|
|
"""
|
|
|
|
Deprecated in favor of `embed`.
|
|
|
|
"""
|
2024-10-16 15:15:10 +08:00
|
|
|
embed_text = []
|
2024-10-21 11:53:06 +00:00
|
|
|
ollama_client = ollama.Client(**kwargs)
|
2024-10-16 15:15:10 +08:00
|
|
|
for text in texts:
|
2024-10-21 11:53:06 +00:00
|
|
|
data = ollama_client.embeddings(model=embed_model, prompt=text)
|
2024-10-16 15:15:10 +08:00
|
|
|
embed_text.append(data["embedding"])
|
|
|
|
|
|
|
|
return embed_text
|
2024-10-14 19:41:07 +08:00
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
|
2024-12-03 08:41:12 +08:00
|
|
|
async def ollama_embed(texts: list[str], embed_model, **kwargs) -> np.ndarray:
|
|
|
|
ollama_client = ollama.Client(**kwargs)
|
|
|
|
data = ollama_client.embed(model=embed_model, input=texts)
|
|
|
|
return data["embeddings"]
|
|
|
|
|
|
|
|
|
2024-10-21 18:34:43 +01:00
|
|
|
class Model(BaseModel):
|
|
|
|
"""
|
|
|
|
This is a Pydantic model class named 'Model' that is used to define a custom language model.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
gen_func (Callable[[Any], str]): A callable function that generates the response from the language model.
|
|
|
|
The function should take any argument and return a string.
|
|
|
|
kwargs (Dict[str, Any]): A dictionary that contains the arguments to pass to the callable function.
|
|
|
|
This could include parameters such as the model name, API key, etc.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
Model(gen_func=openai_complete_if_cache, kwargs={"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY_1"]})
|
|
|
|
|
|
|
|
In this example, 'openai_complete_if_cache' is the callable function that generates the response from the OpenAI model.
|
|
|
|
The 'kwargs' dictionary contains the model name and API key to be passed to the function.
|
|
|
|
"""
|
|
|
|
|
2024-10-25 13:32:25 +05:30
|
|
|
gen_func: Callable[[Any], str] = Field(
|
|
|
|
...,
|
|
|
|
description="A function that generates the response from the llm. The response must be a string",
|
|
|
|
)
|
|
|
|
kwargs: Dict[str, Any] = Field(
|
|
|
|
...,
|
|
|
|
description="The arguments to pass to the callable function. Eg. the api key, model name, etc",
|
|
|
|
)
|
2024-10-21 18:34:43 +01:00
|
|
|
|
|
|
|
class Config:
|
|
|
|
arbitrary_types_allowed = True
|
|
|
|
|
|
|
|
|
2024-10-25 13:32:25 +05:30
|
|
|
class MultiModel:
|
2024-10-21 18:34:43 +01:00
|
|
|
"""
|
|
|
|
Distributes the load across multiple language models. Useful for circumventing low rate limits with certain api providers especially if you are on the free tier.
|
|
|
|
Could also be used for spliting across diffrent models or providers.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
models (List[Model]): A list of language models to be used.
|
|
|
|
|
|
|
|
Usage example:
|
|
|
|
```python
|
|
|
|
models = [
|
|
|
|
Model(gen_func=openai_complete_if_cache, kwargs={"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY_1"]}),
|
|
|
|
Model(gen_func=openai_complete_if_cache, kwargs={"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY_2"]}),
|
|
|
|
Model(gen_func=openai_complete_if_cache, kwargs={"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY_3"]}),
|
|
|
|
Model(gen_func=openai_complete_if_cache, kwargs={"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY_4"]}),
|
|
|
|
Model(gen_func=openai_complete_if_cache, kwargs={"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY_5"]}),
|
|
|
|
]
|
|
|
|
multi_model = MultiModel(models)
|
|
|
|
rag = LightRAG(
|
|
|
|
llm_model_func=multi_model.llm_model_func
|
|
|
|
/ ..other args
|
|
|
|
)
|
|
|
|
```
|
|
|
|
"""
|
2024-10-25 13:32:25 +05:30
|
|
|
|
2024-10-21 18:34:43 +01:00
|
|
|
def __init__(self, models: List[Model]):
|
|
|
|
self._models = models
|
|
|
|
self._current_model = 0
|
2024-10-25 13:32:25 +05:30
|
|
|
|
2024-10-21 18:34:43 +01:00
|
|
|
def _next_model(self):
|
|
|
|
self._current_model = (self._current_model + 1) % len(self._models)
|
|
|
|
return self._models[self._current_model]
|
|
|
|
|
|
|
|
async def llm_model_func(
|
2024-10-25 13:32:25 +05:30
|
|
|
self, prompt, system_prompt=None, history_messages=[], **kwargs
|
2024-10-21 18:34:43 +01:00
|
|
|
) -> str:
|
2024-10-25 13:32:25 +05:30
|
|
|
kwargs.pop("model", None) # stop from overwriting the custom model name
|
2024-12-09 15:35:35 +08:00
|
|
|
kwargs.pop("keyword_extraction", None)
|
|
|
|
kwargs.pop("mode", None)
|
2024-10-21 18:34:43 +01:00
|
|
|
next_model = self._next_model()
|
2024-10-25 13:32:25 +05:30
|
|
|
args = dict(
|
|
|
|
prompt=prompt,
|
|
|
|
system_prompt=system_prompt,
|
|
|
|
history_messages=history_messages,
|
|
|
|
**kwargs,
|
|
|
|
**next_model.kwargs,
|
2024-10-21 18:34:43 +01:00
|
|
|
)
|
2024-10-19 09:43:17 +05:30
|
|
|
|
2024-10-25 13:32:25 +05:30
|
|
|
return await next_model.gen_func(**args)
|
|
|
|
|
|
|
|
|
2024-10-10 15:02:30 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
async def main():
|
2024-10-19 09:43:17 +05:30
|
|
|
result = await gpt_4o_mini_complete("How are you?")
|
2024-10-10 15:02:30 +08:00
|
|
|
print(result)
|
|
|
|
|
2024-11-06 11:18:14 -05:00
|
|
|
asyncio.run(main())
|