File size: 2,684 Bytes
a4b70d9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
from __future__ import annotations
import json
from ..typing import AsyncResult, Messages, MediaListType
from ..client.service import get_model_and_provider
from ..client.helper import filter_json
from .base_provider import AsyncGeneratorProvider
from .response import ToolCalls, FinishReason, Usage
class ToolSupportProvider(AsyncGeneratorProvider):
working = True
@classmethod
async def create_async_generator(
cls,
model: str,
messages: Messages,
stream: bool = True,
media: MediaListType = None,
tools: list[str] = None,
response_format: dict = None,
**kwargs
) -> AsyncResult:
provider = None
if ":" in model:
provider, model = model.split(":", 1)
model, provider = get_model_and_provider(
model, provider,
stream, logging=False,
has_images=media is not None
)
if tools is not None:
if len(tools) > 1:
raise ValueError("Only one tool is supported.")
if response_format is None:
response_format = {"type": "json"}
tools = tools.pop()
lines = ["Respone in JSON format."]
properties = tools["function"]["parameters"]["properties"]
properties = {key: value["type"] for key, value in properties.items()}
lines.append(f"Response format: {json.dumps(properties, indent=2)}")
messages = [{"role": "user", "content": "\n".join(lines)}] + messages
finish = None
chunks = []
has_usage = False
async for chunk in provider.async_create_function(
model,
messages,
stream=stream,
media=media,
response_format=response_format,
**kwargs
):
if isinstance(chunk, str):
chunks.append(chunk)
elif isinstance(chunk, Usage):
yield chunk
has_usage = True
elif isinstance(chunk, FinishReason):
finish = chunk
break
else:
yield chunk
if not has_usage:
yield Usage(completion_tokens=len(chunks), total_tokens=len(chunks))
chunks = "".join(chunks)
if tools is not None:
yield ToolCalls([{
"id": "",
"type": "function",
"function": {
"name": tools["function"]["name"],
"arguments": filter_json(chunks)
}
}])
yield chunks
if finish is not None:
yield finish |