Skip to content

Commit

Permalink
Support Frugal Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
WongSaang committed Apr 6, 2023
1 parent adf415b commit ea78ce8
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def conversation(request):
presence_penalty = request.data.get('presence_penalty', 0)
web_search_params = request.data.get('web_search')
openai_api_key = request.data.get('openaiApiKey')
frugal_mode = request.data.get('frugalMode', False)

if conversation_id:
# get the conversation
Expand All @@ -193,10 +194,10 @@ def conversation(request):
message_obj.save()

try:
messages = build_messages(model, conversation_obj, web_search_params)
messages = build_messages(model, conversation_obj, web_search_params, frugal_mode)

if settings.DEBUG:
print(messages)
print('prompt:', messages)
except Exception as e:
print(e)
return Response(
Expand Down Expand Up @@ -229,6 +230,7 @@ def stream_content():
yield sse_pack('error', {
'error': str(e)
})
print('openai error', e)
return
collected_events = []
completion_text = ''
Expand Down Expand Up @@ -260,11 +262,14 @@ def stream_content():
return StreamingHttpResponse(stream_content(), content_type='text/event-stream')


def build_messages(model, conversation_obj, web_search_params):
def build_messages(model, conversation_obj, web_search_params, frugal_mode = False):

ordered_messages = Message.objects.filter(conversation=conversation_obj).order_by('created_at')
ordered_messages_list = list(ordered_messages)

if frugal_mode:
ordered_messages_list = ordered_messages_list[-1:]

system_messages = [{"role": "system", "content": "You are a helpful assistant."}]

current_token_count = num_tokens_from_messages(system_messages, model['name'])
Expand Down

0 comments on commit ea78ce8

Please sign in to comment.