Skip to content

Newchat context #28

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

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions aistarterkit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@
OPENAI_API_TYPE = os.getenv("OPENAI_API_TYPE", "openai")
OPENAI_API_VERSION = os.getenv("OPENAI_API_VERSION", "2024-10-21")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
OPENAI_API_BASE = os.getenv("OPENAI_API_BASE") # Add this line

# Azure OpenAI Settings
AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
Expand Down
36 changes: 35 additions & 1 deletion chat/ai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
azure_endpoint=settings.AZURE_OPENAI_ENDPOINT,
)
else:
client = OpenAI()
client = OpenAI(
api_key=settings.OPENAI_API_KEY,
base_url=settings.OPENAI_API_BASE
)


class Agent:
Expand Down Expand Up @@ -119,3 +122,34 @@ def _update_history(self, role, content):
Message.objects.create(
thread=self.thread, user=self.thread.user, content=content, role=role
)

def generate_title(self, conversation):
"""Generates a title for the conversation.

Args:
conversation: The first exchange between user and assistant.

Returns:
A string containing the generated title.
"""
prompt = """Based on the conversation below, generate a concise title (10 words or less) that captures the main topic.
Keep it simple and descriptive. Just return the title, nothing else.

Conversation:
{conversation}
"""

messages = [
{"role": "system", "content": prompt.format(conversation=conversation)},
]

try:
completion = client.chat.completions.create(
model=self.thread.model if self.thread else "gpt-3.5-turbo",
messages=messages,
temperature=0,
max_tokens=60
)
return completion.choices[0].message.content.strip()
except Exception as e:
return "New Chat"
27 changes: 16 additions & 11 deletions chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ def thread_detail(request, pk):

@login_required
def create_thread(request):
# Generate a default name for the thread, e.g., "Chat on <current date>"
default_name = f"Chat on {timezone.now().strftime('%Y-%m-%d %H:%M:%S')}"

# Create a new thread with the default name
new_thread = Thread.objects.create(name=default_name, user=request.user)

# Create a new thread with a temporary name
new_thread = Thread.objects.create(
name="New Chat",
user=request.user
)
# Redirect the user to the new thread's detail page
return redirect("thread_detail", pk=new_thread.pk)

Expand All @@ -250,14 +250,19 @@ def new_message(request, pk):
thread = get_object_or_404(Thread, pk=pk)
if request.method == "POST":
form = MessageForm(request.POST)
thread_form = ThreadForm(
request.POST, instance=thread
) # Pass the current thread instance
thread_form = ThreadForm(request.POST, instance=thread)
if form.is_valid() and thread_form.is_valid():
message = form.save(commit=False)
thread = thread_form.save() # Save the thread form to update the thread
thread = thread_form.save()
agent = Agent(thread=thread, prompt=thread.prompt)
agent.chat(message.content)
ai_reply = agent.chat(message.content)

# Generate title if this is the first message in the thread
if thread.message_set.count() <= 2: # User message + AI reply
conversation = f"User: {message.content}\nAssistant: {ai_reply}"
thread.name = agent.generate_title(conversation)
thread.save()

return redirect("thread_detail", pk=thread.pk)
else:
print(form.errors)
Expand Down