Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/chatsequence #13

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions langclient/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ def interactve_chat(
or _prompt_for_api_key()
)

input_name = user_name()
user = user_name()
model_selected = select_language_model()

stream_chat_ = use_key(api_key)(
partial(stream_chat, model=model_selected, max_tokens=3500)
)
stream_chat_ = use_key(api_key)(partial(stream_chat, model=model_selected))

for _ in chat_sequence_process(chat_input(input_name), stream_chat_):
for _ in chat_sequence_process(
chat_input(user), stream_chat_, model_selected, user_name=user
):
pass


Expand Down
21 changes: 13 additions & 8 deletions langclient/chat_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _sum_accumulated_tokens(tokens):
return input_cost + output_cost


def _token_usage_stats(messages: list[Message], model: LanguageModel) -> str:
def token_usage_stats(messages: list[Message], model: LanguageModel) -> str:
input_content = _filter_content_by_role(messages, "user")
output_content = _filter_content_by_role(messages, "assistant")

Expand All @@ -54,8 +54,11 @@ def _token_usage_stats(messages: list[Message], model: LanguageModel) -> str:
usage_stats = f"{_format_token_number(tokens_messages)}/"
usage_stats += f"{_format_token_number(model.max_token)}"

stats_string = f" {Fore.MAGENTA}({usage_stats}){Fore.RESET}"
stats_string += f" {Fore.GREEN}${cost_stats}{Fore.RESET}"
color_token = Fore.LIGHTBLACK_EX
color_cost = Fore.LIGHTGREEN_EX

stats_string = f" {color_token}({usage_stats}){Fore.RESET}"
stats_string += f" {color_cost}${cost_stats}{Fore.RESET}"
return stats_string


Expand All @@ -65,7 +68,6 @@ def stream_chat(
api_key: str,
model: LanguageModel,
temperature=1,
max_tokens=14505,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
Expand All @@ -75,8 +77,7 @@ def stream_chat(
generator: Iterable[ChatCompletionChunk] = client.chat.completions.create(
model=model.name,
messages=list_of_dict_messages,
temperature=temperature,
max_tokens=max_tokens,
temperature=temperature, # next parameters have no effect
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
Expand All @@ -85,9 +86,13 @@ def stream_chat(

deltas_content = map(lambda chunk: chunk.choices[0].delta.content, generator)

assistant_head = f"\n{Fore.CYAN}Assistant:{Fore.RESET}"
assistant_head += _token_usage_stats(messages, model)
color_assistant = Fore.BLUE
assistant_name = "Assistant"

assistant_head = f"{color_assistant}{assistant_name}:{Fore.RESET}"
assistant_head += token_usage_stats(messages, model)

print()
print(assistant_head)

def _typeguard(content: str | None) -> TypeGuard[str]:
Expand Down
8 changes: 1 addition & 7 deletions langclient/data/models.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
"cost_1kT_output": 0.0015
},
{
"name": "gpt-4-0125-preview",
"max_token": 128000,
"cost_1kT_input": 0.01,
"cost_1kT_output": 0.03
},
{
"name": "gpt-4-1106-preview",
"name": "gpt-4-turbo-2024-04-09",
"max_token": 128000,
"cost_1kT_input": 0.01,
"cost_1kT_output": 0.03
Expand Down
35 changes: 29 additions & 6 deletions langclient/interactive_chat_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from itertools import accumulate
from functools import partial

from langclient.models import Message, Role
from langclient.models import Message, Role, LanguageModel
from langclient.chat_functions import token_usage_stats


def _get_files_from_message(message: str) -> list[str]:
Expand Down Expand Up @@ -53,7 +54,6 @@ def chat_input(user_name: str) -> Iterable[Message]:
"""Chat input stream."""

while True:
print(Fore.CYAN + f"{user_name}:" + Fore.RESET)
input_message = input()
yield _enhance_user_input(input_message)

Expand All @@ -79,7 +79,11 @@ def _print_intercept(iter: Iterable):


def _step_process(
previous_messages: list[Message], user_message: Message, chat_function: Callable
previous_messages: list[Message],
user_message: Message,
chat_function: Callable,
model: LanguageModel,
user_name: str,
) -> list[Message]:
"""Get the next step in the conversation."""
assistant_response_chunks = chat_function([*previous_messages, user_message])
Expand All @@ -88,13 +92,32 @@ def _step_process(
role=Role.ASSISTANT,
content="".join(_print_intercept(assistant_response_chunks)),
)

chat_updated = [*previous_messages, user_message, assistant_response]

user_name_color = Fore.CYAN
user_head = f"{user_name_color}{user_name}:{Fore.RESET}"
user_head += token_usage_stats(chat_updated, model)

print()
print()
return [*previous_messages, user_message, assistant_response]
print(user_head)

return chat_updated


def chat_sequence_process(
user_input: Iterable[Message], chat_function: Callable
user_input: Iterable[Message],
chat_function: Callable,
model: LanguageModel,
user_name: str,
) -> Iterable[Message]:
accumulate_function = partial(_step_process, chat_function=chat_function)
accumulate_function = partial(
_step_process, chat_function=chat_function, user_name=user_name, model=model
)

user_name_color = Fore.CYAN
first_user_head = f"{user_name_color}{user_name}:{Fore.RESET}"
print(first_user_head)

return accumulate(user_input, accumulate_function, initial=[])
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="langclient",
version="0.4.0",
version="0.4.1",
description="Simple OpenAI language repl client",
author="Juan Molina Riddell",
author_email="[email protected]",
Expand Down
Loading