llama.cpp/llama_cpp/llama_types.py

173 lines
3.9 KiB
Python
Raw Normal View History

"""Types and request signatrues for OpenAI compatibility
Based on the OpenAI OpenAPI specification:
https://github.com/openai/openai-openapi/blob/master/openapi.yaml
"""
from typing import Any, List, Optional, Dict, Union
2023-04-05 08:37:33 +00:00
from typing_extensions import TypedDict, NotRequired, Literal
2023-04-01 16:59:58 +00:00
class EmbeddingUsage(TypedDict):
prompt_tokens: int
total_tokens: int
class Embedding(TypedDict):
2023-04-01 16:59:58 +00:00
index: int
object: str
embedding: List[float]
EmbeddingData = Embedding
class CreateEmbeddingResponse(TypedDict):
2023-04-01 16:59:58 +00:00
object: Literal["list"]
model: str
data: List[Embedding]
2023-04-01 16:59:58 +00:00
usage: EmbeddingUsage
class CompletionLogprobs(TypedDict):
text_offset: List[int]
token_logprobs: List[Optional[float]]
2023-04-01 16:59:58 +00:00
tokens: List[str]
top_logprobs: List[Optional[Dict[str, float]]]
2023-04-01 16:59:58 +00:00
class CompletionChoice(TypedDict):
text: str
index: int
logprobs: Optional[CompletionLogprobs]
finish_reason: Optional[Literal["stop", "length"]]
2023-04-01 16:59:58 +00:00
class CompletionUsage(TypedDict):
prompt_tokens: int
completion_tokens: int
total_tokens: int
class CreateCompletionStreamResponse(TypedDict):
2023-04-01 16:59:58 +00:00
id: str
object: Literal["text_completion"]
created: int
model: str
choices: List[CompletionChoice]
CompletionChunk = CreateCompletionStreamResponse
class CreateCompletionResponse(TypedDict):
2023-04-01 16:59:58 +00:00
id: str
object: Literal["text_completion"]
created: int
model: str
choices: List[CompletionChoice]
usage: CompletionUsage
2023-04-04 00:12:44 +00:00
Completion = CreateCompletionResponse
class ChatCompletionFunctionCall(TypedDict):
name: str
arguments: str
class ChatCompletionResponseMessage(TypedDict):
role: Literal["assistant", "user", "system", "function"]
content: Optional[str]
user: NotRequired[str]
function_call: NotRequired[ChatCompletionFunctionCall]
ChatCompletionMessage = ChatCompletionResponseMessage
2023-04-04 00:12:44 +00:00
class ChatCompletionResponseFunction(TypedDict):
2023-07-19 07:48:20 +00:00
name: str
description: NotRequired[str]
parameters: Dict[str, Any] # TODO: make this more specific
ChatCompletionFunction = ChatCompletionResponseFunction
2023-07-19 07:48:20 +00:00
class ChatCompletionResponseChoice(TypedDict):
2023-04-04 00:12:44 +00:00
index: int
message: ChatCompletionMessage
finish_reason: Optional[str]
ChatCompletionChoice = ChatCompletionResponseChoice
class CreateChatCompletionResponse(TypedDict):
2023-04-04 00:12:44 +00:00
id: str
object: Literal["chat.completion"]
created: int
model: str
choices: List[ChatCompletionChoice]
usage: CompletionUsage
2023-07-19 07:48:27 +00:00
ChatCompletion = CreateChatCompletionResponse
class ChatCompletionStreamResponseDeltaEmpty(TypedDict):
pass
2023-04-04 00:12:44 +00:00
2023-07-19 07:48:27 +00:00
ChatCompletionChunkDeltaEmpty = ChatCompletionStreamResponseDeltaEmpty
class ChatCompletionStreamResponseDelta(TypedDict):
2023-04-04 00:12:44 +00:00
role: NotRequired[Literal["assistant"]]
content: NotRequired[str]
function_call: NotRequired[ChatCompletionFunctionCall]
ChatCompletionChunkDelta = ChatCompletionStreamResponseDelta
2023-04-04 00:12:44 +00:00
class ChatCompletionStreamResponseChoice(TypedDict):
2023-04-04 00:12:44 +00:00
index: int
delta: Union[ChatCompletionChunkDelta, ChatCompletionChunkDeltaEmpty]
finish_reason: Optional[Literal["stop", "length", "function_call"]]
ChatCompletionChunkChoice = ChatCompletionStreamResponseChoice
2023-04-04 00:12:44 +00:00
class ChatCompletionStreamResponse(TypedDict):
2023-04-04 00:12:44 +00:00
id: str
model: str
object: Literal["chat.completion.chunk"]
created: int
choices: List[ChatCompletionChunkChoice]
ChatCompletionChunk = ChatCompletionStreamResponse
JsonType = Union[None, int, str, bool, List["JsonType"], Dict[str, "JsonType"]]
class ChatCompletionFunctions(TypedDict):
name: str
description: NotRequired[str]
parameters: Dict[str, JsonType] # TODO: make this more specific
class ChatCompletionFunctionCallOption(TypedDict):
name: str
class ChatCompletionRequestMessage(TypedDict):
role: Literal["assistant", "user", "system", "function"]
content: Optional[str]
name: NotRequired[str]
funcion_call: NotRequired[ChatCompletionFunctionCall]