Update README.md
Browse files
README.md
CHANGED
|
@@ -21,24 +21,48 @@ This gemma2 model was trained 2x faster with [Unsloth](https://github.com/unslot
|
|
| 21 |
|
| 22 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
| 23 |
|
| 24 |
-
-- teste esse codigo com o prompt modificado para realizar chamadas de funcao
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
from langchain.agents import AgentExecutor
|
| 28 |
-
from langchain.agents import tool,
|
| 29 |
from langchain import hub
|
| 30 |
-
import os
|
| 31 |
from langchain_ollama.llms import OllamaLLM
|
| 32 |
from langchain.prompts import PromptTemplate
|
| 33 |
|
| 34 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
@tool
|
| 36 |
def get_word_length(word: str) -> int:
|
| 37 |
-
"""
|
| 38 |
return len(word)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
custom_react_prompt = PromptTemplate(
|
| 43 |
input_variables=["input", "agent_scratchpad", "tools", "tool_names"],
|
| 44 |
template="""Answer the following questions as best you can. You have access to the following tools:
|
|
@@ -70,12 +94,12 @@ Question: {input}
|
|
| 70 |
Thought: {agent_scratchpad}"""
|
| 71 |
)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
tools = [get_word_length]
|
| 75 |
tools_str = "\n".join([f"{tool.name}: {tool.description}" for tool in tools])
|
| 76 |
tool_names = ", ".join([tool.name for tool in tools])
|
| 77 |
|
| 78 |
-
# Criar o agente
|
| 79 |
agent = create_react_agent(
|
| 80 |
tools=tools,
|
| 81 |
llm=llm,
|
|
@@ -90,7 +114,10 @@ agent_executor = AgentExecutor(
|
|
| 90 |
handle_parsing_errors=True
|
| 91 |
)
|
| 92 |
|
| 93 |
-
# Testar
|
| 94 |
-
question = "What is the length of the word PythonDanelonAugustoTrajanoRomanovCzarVespasianoDiocleciano
|
| 95 |
response = agent_executor.invoke({"input": question})
|
| 96 |
print(response)
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
| 23 |
|
|
|
|
| 24 |
|
| 25 |
+
---
|
| 26 |
+
|
| 27 |
+
```markdown
|
| 28 |
+
# Agente de Chamada de Função com LangChain e Prompt Personalizado
|
| 29 |
+
|
| 30 |
+
Este projeto implementa um agente baseado em LangChain com um prompt personalizado para realizar chamadas de função, utilizando o modelo `GEMMA-2-2B-it-GGUF-function_calling` hospedado no Hugging Face.
|
| 31 |
+
|
| 32 |
+
## Descrição
|
| 33 |
+
|
| 34 |
+
O código cria um agente que utiliza ferramentas personalizadas e um modelo de linguagem para responder perguntas com base em um fluxo estruturado de pensamento e ação. Ele inclui uma ferramenta personalizada (`get_word_length`) que calcula o comprimento de uma palavra e um prompt ReAct modificado para guiar o raciocínio do agente.
|
| 35 |
+
|
| 36 |
+
## Pré-requisitos
|
| 37 |
+
|
| 38 |
+
- Python 3.8+
|
| 39 |
+
- Bibliotecas necessárias:
|
| 40 |
+
```bash
|
| 41 |
+
pip install langchain langchain-ollama
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
## Código
|
| 45 |
+
|
| 46 |
+
Aqui está o código principal:
|
| 47 |
+
|
| 48 |
+
```python
|
| 49 |
from langchain.agents import AgentExecutor
|
| 50 |
+
from langchain.agents import tool, create_react_agent
|
| 51 |
from langchain import hub
|
|
|
|
| 52 |
from langchain_ollama.llms import OllamaLLM
|
| 53 |
from langchain.prompts import PromptTemplate
|
| 54 |
|
| 55 |
+
# Definir o modelo
|
| 56 |
+
MODEL = "hf.co/vinimuchulski/GEMMA-2-2B-it-GGUF-function_calling:latest"
|
| 57 |
+
llm = OllamaLLM(model=MODEL)
|
| 58 |
+
|
| 59 |
+
# Criar ferramenta personalizada
|
| 60 |
@tool
|
| 61 |
def get_word_length(word: str) -> int:
|
| 62 |
+
"""Retorna o comprimento de uma palavra."""
|
| 63 |
return len(word)
|
| 64 |
|
| 65 |
+
# Definir prompt personalizado
|
|
|
|
| 66 |
custom_react_prompt = PromptTemplate(
|
| 67 |
input_variables=["input", "agent_scratchpad", "tools", "tool_names"],
|
| 68 |
template="""Answer the following questions as best you can. You have access to the following tools:
|
|
|
|
| 94 |
Thought: {agent_scratchpad}"""
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# Configurar ferramentas
|
| 98 |
tools = [get_word_length]
|
| 99 |
tools_str = "\n".join([f"{tool.name}: {tool.description}" for tool in tools])
|
| 100 |
tool_names = ", ".join([tool.name for tool in tools])
|
| 101 |
|
| 102 |
+
# Criar o agente
|
| 103 |
agent = create_react_agent(
|
| 104 |
tools=tools,
|
| 105 |
llm=llm,
|
|
|
|
| 114 |
handle_parsing_errors=True
|
| 115 |
)
|
| 116 |
|
| 117 |
+
# Testar o agente
|
| 118 |
+
question = "What is the length of the word PythonDanelonAugustoTrajanoRomanovCzarVespasianoDiocleciano?"
|
| 119 |
response = agent_executor.invoke({"input": question})
|
| 120 |
print(response)
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
|