How would I swap in the "ollama/llama2" model into the RestrictToTopic validator #1219
-
I hope you can assist me. I'm trying to swap in a different model to the RestrictToTopic using llmlite. From the Documentation I think I have the right approach. I have the folloing code :
hen I run this code, I get an error:
Can anyone point out where I am going wrong. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @widgetface, the answer depends on how you intend to use ollama/llama2 within the validator. By default, this validator uses both a zero-shot classification model and an LLM model to perform an "ensemble" approach when extracting topics from the provided text. Using Only ollama/llama2If you want to use only ollama/llama2 to perform this function, you should disable the zero-shot model by passing def openai_callable(text: str, topics: List[str]) -> str:
api_key, api_base = self.get_client_args()
client = OpenAI(api_key=api_key, base_url=api_base)
response = client.chat.completions.create(
model=llm_callable,
response_format={"type": "json_object"},
messages=[
{
"role": "user",
"content": f"""
Given a text and a list of topics, return a valid json list of which topics are present in the text. If none, just return an empty list.
Output Format:
-------------
"topics_present": []
Text:
----
"{text}"
Topics:
------
{topics}
Result:
------ """,
},
],
)
return json.loads(response.choices[0].message.content)["topics_present"] Using ollama/llama2 Alongside Zero-Shot ClassificationIf you want to continue using the default ensemble approach but swap out gpt-4o for ollama/llama2, you can do as I said above but leave Using ollama/llama2 As The Zero-Shot ClassificationThis is not possible because ollama/llama2 is not a transformers pipeline compatible zero-shot-classification model. However, you can disable the zero-shot classification like I described in the first scenario, or you can choose an appropriate zero-shot-classification model from here: https://huggingface.co/models?pipeline_tag=zero-shot-classification |
Beta Was this translation helpful? Give feedback.
Hi @widgetface, the answer depends on how you intend to use ollama/llama2 within the validator. By default, this validator uses both a zero-shot classification model and an LLM model to perform an "ensemble" approach when extracting topics from the provided text.
Using Only ollama/llama2
If you want to use only ollama/llama2 to perform this function, you should disable the zero-shot model by passing
disable_classifier=True
and set thellm_callable
to a Callable that takes the text to be validated and a list of topics, and returns a string with any topics found. Below is the default method that does this with an OpenAI model. Yours will likely look similar but with litellm and your chosen …