ollama / ollama-python

Ollama Python library
https://ollama.com
MIT License
4.09k stars 344 forks source link

Is there a way for using chat() function by passing more context ? #48

Closed unexpand closed 8 months ago

unexpand commented 8 months ago

What I am trying to attempt is something like langchain RetrievalQA, making chat request with some context. I got the documents from pgvector postgres using search and I want to use prompt like "Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer." and pass the documents as context so mistral llm can use the context to respond.

my code currently looks like this and it works, but I am not sure if this is a valid approach.

async def chat( prompt:string, content:any):
  template = "Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer."
  {content}
  Question: {prompt}
  QA_CHAIN_PROMPT = PromptTemplate(input_variables=["context", "question"], template=template,)

  newTemplate = template + "\n" + "Question: " + prompt + "\n" + content 

  message = {'role': 'user', 'content': newTemplate }
  print(prompt, end='', flush=True)
  async for part in await AsyncClient().chat(model='mistral', messages=[message], stream=True):
    print(part['message']['content'], end='', flush=True)
mxyng commented 8 months ago

chat is intended to be used with ollama's model templates with messages representing one side of a chat exchange. To better illustrate this here's an example equivalent to your code:

messages = [
  {
    "role": "system",
    "content": "Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer."
  },
  {
    "role": "user",
    "content": prompt,
  },
]

async for part in AsyncClient().chat(model='mistral', messages=messages, stream=True):
  print(part['message']['content'], end='', flush=True)

The input messages can be construct anyway you want however the only valid roles are system, user, and assistant.