langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Add "name" parameter in Request body under messages for chat completions via OpenAI API

Open Smuglix opened this issue 2 years ago • 8 comments

Add "name" parameter in Request body under messages for chat completions via OpenAI API

This adds "name" parameter to the request body inside "messages". For example, when defining HumanMessage or AIMessage, there is now option to add name parameter like this:

 HumanMessage(content="Hello! what is my name?", name='Ilya')

As in OpenAI API: https://platform.openai.com/docs/api-reference/chat/create#chat/create-name image

The message dictionaries look like this (of course name parameter is optional):

{'role': 'system', 'content': "That's goofy conversation without rules"}
{'role': 'user', 'name': 'Spy', 'content': 'Hello! what is my name? And your name?'}
{'role': 'assistant', 'name': 'Scout', 'content': "Hi! I won't answer your question, You mad? :P "}
{'role': 'assistant', 'name': 'Heavy', 'content': "Bruh man, don't be mean"}
{'role': 'user', 'name': 'spy', 'content': 'As you wish, Scout. Off to visit your mom ヾ(•ω•`)o'}
{'role': 'system', 'content': "summarize the conversation and list it's participants"}

Simple test

import os
from dotenv import load_dotenv
load_dotenv()
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate, LLMChain
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)

chat = ChatOpenAI(model_name="gpt-4",temperature=1) # change gpt-4 to gpt-3.5-turbo if you doesn't have gpt-4 access yet
messages = [
    SystemMessage(content="That's goofy conversation without rules"),
    HumanMessage(content="Hello! what is my name? And your name?", name='Spy'),
    AIMessage(content="Hi! I won't answer your question, You mad? :P ", name='Scout'),
    AIMessage(content="Bruh man, don't be mean", name='Heavy'),
    HumanMessage(content="As you wish, Scout. Off to visit your mom ヾ(•ω•`)o", name='spy'),
    SystemMessage(content="summarize the conversation and list it's participants")

]
response=chat(messages)

print(response.content,end='\n')

Response should be smt like this:

Participants: User (Spy), Assistant (Scout), Assistant (Heavy)

Summary: The User initiates a goofy conversation asking about names. Assistant (Scout) responds playfully by not answering the question, then Assistant (Heavy) advises to not be mean. The User then continues with a witty remark about visiting Assistant (Scout)'s mom.

Maintainers/contributors who might be interested:

Models @hwchase17 @agola11

VectorStores / Retrievers / Memory @dev2049

Smuglix avatar May 31 '23 15:05 Smuglix

yeah i saw we were missing this. what is the main use you have in mind for this?

hwchase17 avatar May 31 '23 22:05 hwchase17

yeah i saw we were missing this. what is the main use you have in mind for this?

The main use is in group chats. It's important for gpt to know that there are different people chatting with it. I'm currently using it for discord bot. It's much more convenient than trying to put names in the content of message, which broke prompts for it.

For example, if you put username in content of the message (Ilya: Hello! what is my name?), It will start to respond in similar format, or even for other people. You can fix it with prompt engineering, but the above feature is just much more convenient.

Smuglix avatar Jun 01 '23 04:06 Smuglix

@hwchase17 I see some checks weren't successful, do I need to worry about it?

Smuglix avatar Jun 01 '23 05:06 Smuglix

I just don't know how to fix this, the error message in lint doesn't make sense to me

Smuglix avatar Jun 01 '23 05:06 Smuglix

Nice find! Can you make sure that we can also incorporate different assistants (e.g. add a name parameter to the AIMessage as well). This would enable some fun multi user - multi-bot scenarios.

vbandi avatar Jun 02 '23 22:06 vbandi

Nice find! Can you make sure that we can also incorporate different assistants (e.g. add a name parameter to the AIMessage as well). This would enable some fun multi user - multi-bot scenarios.

You are right! Gonna make it tonight

Smuglix avatar Jun 02 '23 23:06 Smuglix

@hwchase17 I updated the files, thanks to @vbandi for noticing about AIMessage. I also noticed that I forgot to update the function that fetches messages from dictionaries, to include names. It's fixed now, and I think that this pr is ready.

Smuglix avatar Jun 03 '23 13:06 Smuglix

Also updated the first message in this pr to be relevant. The test now simulates group chat, and showcases the feature

Smuglix avatar Jun 03 '23 13:06 Smuglix

@Smuglix any updates?

n-splv avatar Aug 07 '23 08:08 n-splv

@Smuglix any updates?

I decided that I won't use langchain, because I think that it overcomplicates things compared to openai's api. So I stopped working on this.

Smuglix avatar Aug 07 '23 08:08 Smuglix

^ the last comment from @Smuglix

leo-gan avatar Sep 13 '23 20:09 leo-gan