Skip to content

Commit

Permalink
Update:
Browse files Browse the repository at this point in the history
• Implemented minor adjustments to enhance Langchain compatibility.
• Increased example quantity for improved functionality identification.
• Updated tool detection handling; "content" is now set to an empty string, as opposed to "None" due to Langchain library.
• Introduced a "groqchain" endpoint.
• Provided an additional example.
  • Loading branch information
unclecode committed Mar 20, 2024
1 parent d5ee25c commit 309b351
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# To be developed
EVALUATION_CYCLES_COUNT=3
EVALUATION_CYCLES_COUNT=1
PARSE_ERROR_TRIES = 5
2 changes: 1 addition & 1 deletion app/libs/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def is_tool_call(self):
return bool(
self.last_message["role"] == "user"
and self.tools
and self.params.get("tool_choice", "none") != "none"
and self.params.get("tool_choice", None) != "none"
)

@property
Expand Down
2 changes: 1 addition & 1 deletion app/libs/tools_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def call_route(messages):
for func in tool_calls:
tool_calls_count[func["name"]] += 1

pickup_threshold = max(evaluation_cycles_count, int(evaluation_cycles_count * 0.7))
pickup_threshold = math.floor(evaluation_cycles_count * 0.7)
final_tool_calls = []
for tool_calls in tool_calls_list:
for func in tool_calls:
Expand Down
208 changes: 205 additions & 3 deletions app/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,213 @@ def get_image_desc_guide(ref_index: int, description: str) -> str:
}
</selected_tools>""",
},
]
{
"input": """# Conversation History:
<user>
What was the closing price of Apple Inc. (AAPL) stock yesterday?
</user>
# Available Tools:
[
{
"name": "get_stock_price",
"description": "Use this function to retrieve the latest price information for a given stock symbol.\n\n:param symbol: The stock symbol to retrieve the price for (e.g., 'AAPL' for Apple Inc.).\n:return: A string containing the latest price information for the specified stock.",
"parameters": {
"type": "object",
"properties": {
"symbol": {
"type": "string"
}
}
}
},
{
"name": "get_stock_history",
"description": "Use this function to retrieve the historical price data for a given stock symbol over a specified period.\n\n:param symbol: The stock symbol to retrieve the historical data for.\n:param period: The time period for the historical data (e.g., '1d', '5d', '1mo', '1y').\n:return: A string containing the historical price data for the specified stock and period.",
"parameters": {
"type": "object",
"properties": {
"symbol": {
"type": "string"
},
"period": {
"type": "string"
}
}
}
}
]""",
"output": """<justification>
The user is asking for the closing price of Apple Inc. (AAPL) stock from the previous day. We can use the "get_stock_price" tool to retrieve the latest price information for the AAPL stock symbol. The "get_stock_history" tool is not needed in this case, as the user is only asking for the closing price from yesterday.
</justification>
<selected_tools>
{
"tool_calls": [
{
"name": "get_stock_price",
"arguments": {
"symbol": "AAPL"
}
}
]
}
</selected_tools>""",
},
{
"input": """# Conversation History:
<user>
I'm considering investing in Tesla (TSLA) stock. Can you provide me with some information to help me make a decision?
</user>
<assistant>
To help you make an informed decision about investing in Tesla (TSLA) stock, I can provide you with the following information:
1. Latest price: The current price of TSLA stock can give you an idea of its market value.
2. Historical performance: Analyzing the stock's price history over different periods (e.g., 1 month, 6 months, 1 year) can help you understand its volatility and potential for growth.
3. Company fundamentals: Researching Tesla's financial statements, revenue growth, profitability, and future projects can provide insights into the company's overall health and potential.
4. Market sentiment: Monitoring news, analyst ratings, and investor sentiment can help gauge the market's perception of Tesla stock.
Let me know which specific information you'd like me to provide first, and I'll be happy to assist you further.
</assistant>
<user>
Thanks for the overview. Let's start with the latest price and the historical performance over the past 6 months. That should give me a good starting point.
</user>
# Available Tools:
[
{
"name": "get_stock_price",
"description": "Use this function to retrieve the latest price information for a given stock symbol.\n\n:param symbol: The stock symbol to retrieve the price for (e.g., 'AAPL' for Apple Inc.).\n:return: A string containing the latest price information for the specified stock.",
"parameters": {
"type": "object",
"properties": {
"symbol": {
"type": "string"
}
}
}
},
{
"name": "get_stock_history",
"description": "Use this function to retrieve the historical price data for a given stock symbol over a specified period.\n\n:param symbol: The stock symbol to retrieve the historical data for.\n:param period: The time period for the historical data (e.g., '1d', '5d', '1mo', '1y').\n:return: A string containing the historical price data for the specified stock and period.",
"parameters": {
"type": "object",
"properties": {
"symbol": {
"type": "string"
},
"period": {
"type": "string"
}
}
}
}
]""",
"output": """<justification>
The user has requested two specific pieces of information to help them make a decision about investing in Tesla (TSLA) stock: the latest price and the historical performance over the past 6 months. To provide this information, we need to use both the "get_stock_price" and "get_stock_history" tools.
1. "get_stock_price" will be used to retrieve the current price of TSLA stock.
2. "get_stock_history" will be used to retrieve the price history of TSLA stock over the past 6 months (period: '6mo').
By combining the results from these two tools, we can give the user the requested information to support their investment decision.
</justification>
<selected_tools>
{
"tool_calls": [
{
"name": "get_stock_price",
"arguments": {
"symbol": "TSLA"
}
},
{
"name": "get_stock_history",
"arguments": {
"symbol": "TSLA",
"period": "6mo"
}
}
]
}
</selected_tools>""",
},
{
"input": """# Conversation History:
<user>
What do you think about the recent performance of the stock market?
</user>
<assistant>
The stock market's recent performance has been mixed, with some sectors showing strong growth while others have experienced volatility. Here are a few key points:
1. Tech stocks: Many technology companies have seen significant gains, driven by strong earnings and optimism about the sector's long-term prospects.
2. Energy sector: Oil and gas stocks have been under pressure due to fluctuating crude oil prices and concerns about the global economic recovery.
3. Health care: The health care sector has been a mixed bag, with some companies benefiting from the focus on COVID-19 treatments and vaccines, while others have faced challenges.
4. Overall market: The broader stock market indices, such as the S&P 500 and Dow Jones Industrial Average, have reached record highs recently, supported by positive economic data and optimism about the post-pandemic recovery.
It's important to remember that the stock market's performance is influenced by various factors, including economic indicators, geopolitical events, and investor sentiment. As an AI language model, I cannot provide personalized investment advice, but I suggest researching specific sectors and companies, and consulting with a financial advisor before making investment decisions.
</assistant>
<user>
Thanks for the insights. It's helpful to get a general overview of the market's performance across different sectors. I'll definitely do more research and consult with a financial advisor before making any investment decisions.
</user>
# Available Tools:
[
{
"name": "get_stock_price",
"description": "Use this function to retrieve the latest price information for a given stock symbol.\n\n:param symbol: The stock symbol to retrieve the price for (e.g., 'AAPL' for Apple Inc.).\n:return: A string containing the latest price information for the specified stock.",
"parameters": {
"type": "object",
"properties": {
"symbol": {
"type": "string"
}
}
}
},
{
"name": "get_stock_history",
"description": "Use this function to retrieve the historical price data for a given stock symbol over a specified period.\n\n:param symbol: The stock symbol to retrieve the historical data for.\n:param period: The time period for the historical data (e.g., '1d', '5d', '1mo', '1y').\n:return: A string containing the historical price data for the specified stock and period.",
"parameters": {
"type": "object",
"properties": {
"symbol": {
"type": "string"
},
"period": {
"type": "string"
}
}
}
}
]""",
"output": """<justification>
The user's question about the recent performance of the stock market is broad and does not require the use of the available tools. The conversation history shows that the assistant has provided a general overview of the market's performance across different sectors, which sufficiently addresses the user's question. The user also acknowledges that the insights are helpful and expresses their intention to conduct further research and consult with a financial advisor. As a result, no specific tools are needed to answer this query.
</justification>
<selected_tools>
{
"tool_calls": []
}
</selected_tools>""",
},
]

import random
def get_suffix():
random.shuffle(FEW_SHOTS)
# Turn each element of FEW_SHOTS into a string like '-- EXAMPLE i ---\nINPUT:\n{input}\n\nOUTPUT:\n{output}\n\n', then join them by \n\n
few_shots = "\n\n".join([f'-- EXAMPLE {i} ---\nINPUT:\n{example["input"]}\n\nOUTPUT:\n{example["output"]}\n\n' for i, example in enumerate(FEW_SHOTS, 1)])
few_shots = "\n\n".join(
[
f'-- EXAMPLE {i} ---\nINPUT:\n{example["input"]}\n\nOUTPUT:\n{example["output"]}\n\n'
for i, example in enumerate(FEW_SHOTS, 1)
]
)
# Replace FEW_SHOTS with the actual examples
return SUFFIX.replace("FEW_SHOTS", few_shots)
return SUFFIX.replace("FEW_SHOTS", few_shots)
2 changes: 1 addition & 1 deletion app/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def clean_params(self, params):
pass


class OpenAIProvider(BaseProvider):
class OpenaiProvider(BaseProvider):
def __init__(self, api_key: str, base_url = None):
super().__init__(api_key)
self._client = OpenAI(api_key=api_key)
Expand Down
9 changes: 9 additions & 0 deletions app/routes/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ async def get_openai_v1(
return JSONResponse(content={"message": f"GET request to {provider} v1"})


@router.post("/groqchain/{provider}/v1/chat/completions")
async def post_groq_chat_completions(
request: Request,
provider: str = Path(..., title="Provider")
) -> JSONResponse:
# Call the original post_chat_completions method with provider set to "groq"
return await post_chat_completions(request, provider="groq")


@router.post("/{provider}/v1/chat/completions")
async def post_chat_completions(
request: Request,
Expand Down
2 changes: 1 addition & 1 deletion app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_tool_call_response(tool_calls_result, unresolved_tol_calls, resolved_res
"index": 0,
"message": {
"role": "assistant",
"content": None,
"content": "", # None,
"tool_calls": unresolved_tol_calls,
},
"logprobs": None,
Expand Down
53 changes: 53 additions & 0 deletions cookbook/functiona_call_groq_langchain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# pip install --upgrade --quiet langchain-groq tavily-python langchain langchainhub langchain-openai

from langchain_core.prompts import ChatPromptTemplate
from langchain_groq import ChatGroq
import os
from dotenv import load_dotenv
load_dotenv()
from langchain import hub
from langchain.agents import create_openai_tools_agent
from langchain_community.tools.tavily_search import TavilySearchResults, TavilyAnswer
from langchain.agents import AgentExecutor

# The following code raise an error.
chat = ChatGroq(
temperature=0,
groq_api_key=os.environ["GROQ_API_KEY"],
model_name="mixtral-8x7b-32768",
)

prompt = hub.pull("hwchase17/openai-tools-agent")
tools = [TavilySearchResults(max_results=1)]
agent = create_openai_tools_agent(chat, tools, prompt)

agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, stream_runnable = False)
agent_executor.invoke({"input": "What is Langchain?"})


# The following code works fine using GroqCall (Funckycall) proxy
chat = ChatGroq(
temperature=0,
groq_api_key=os.environ["GROQ_API_KEY"],
model_name="mixtral-8x7b-32768",
groq_api_base= "http://localhost:8000/proxy/groqchain"
# groq_api_base= "http://groqcall.ai/proxy/groqchain"
)

# Example 1: Chat with tools
prompt = hub.pull("hwchase17/openai-tools-agent")
tools = [TavilySearchResults(max_results=1)]
agent = create_openai_tools_agent(chat, tools, prompt)

agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, stream_runnable = False)
result = agent_executor.invoke({"input": "What is Langchain?"})
print(result)

# Example 1: Simple chat
system = "You are a helpful assistant."
human = "{text}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])

chain = prompt | chat
result = chain.invoke({"text": "Explain the importance of low latency LLMs."})
print(result)

0 comments on commit 309b351

Please sign in to comment.