pythonnlpopenai-apilangchainchat-gpt-4

Get chatGPT to respond with a single direct answer


I am querying a text using chatGPT. But I need chatGPT to respond with single direct answers, rather than long stories or irrelevant text. Any way to achieve this?

My code looks like:

from langchain.document_loaders import TextLoader
from langchain.vectorstores import DocArrayInMemorySearch
from langchain.indexes import VectorstoreIndexCreator

loader = TextLoader("path/to/extracted_text.txt")
loaded_text = loader.load()
# Save document text as vector.
index = VectorstoreIndexCreator(
            vectorstore_cls=DocArrayInMemorySearch
        ).from_loaders([loader])

# Query the text
response = index.query("At what time did john come home yesterday?")
print("Loaded text is:", loaded_text)
print("ChatGPT response is:", response)

>>> Loaded text is: "< a really long text > + John came home last night at 11:30pm + < a really long text >"

>>> ChatGPT response is: "John came back yesterday at 11:30pm."

The problem is that I want a concise answer 11:30pm rather than a full sentence John came home last night at 11:30pm. Is there a way to achieve this without adding "I need a short direct response" to my query? Can I achieve a more guaranteed concise response by setting a parameter through some other means instead?


Solution

  • The BEST way to achieve what you want is Proper Prompt Engineering. Period. No way around it. It's more of a thought discipline than acquiring a new skill. Read this quick Microsoft Learn doc about prompt engineering to privy yourself to the knowledge you need to advance your task. Best wishes!