langchain
langchain copied to clipboard
Add "name" parameter in Request body under messages for chat completions via OpenAI API
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
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
yeah i saw we were missing this. what is the main use you have in mind for this?
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.
@hwchase17 I see some checks weren't successful, do I need to worry about it?
I just don't know how to fix this, the error message in lint doesn't make sense to me
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.
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
@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.
Also updated the first message in this pr to be relevant. The test now simulates group chat, and showcases the feature
@Smuglix any updates?
@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.
^ the last comment from @Smuglix