Check out other tutorials:LLMs - Resources & Tutorials


Code base: Python

Image Recognition: Azure Cognitive Services

Model: gpt-3.5-turbo-instruct

import os
from langchain.llms import OpenAI
from dotenv import load_dotenv
from langchain.agents.agent_toolkits import AzureCognitiveServicesToolkit
from langchain.agents import initialize_agent, AgentType

load_dotenv()

#image key
os.environ["AZURE_COGS_KEY"] = ""
os.environ["AZURE_COGS_ENDPOINT"] = ""
os.environ["AZURE_COGS_REGION"] = ""

toolkit = AzureCognitiveServicesToolkit()
llm = OpenAI(temperature=0, model="gpt-3.5-turbo-instruct")
agent = initialize_agent(
    tools=[tool for tool in toolkit.get_tools() if tool.name == 'azure_cognitive_services_image_analysis'],
    llm=llm,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)
 
result = agent.run(
    """
    Below is a link to a screenshot of a FB ad. Analyze it and think if it's scam or legit. Be a critical thinker and output your answer as: Scam/Legit, your confidence score, brief reasoning.
    <https://drive.google.com/uc?id={id}>""")
print("Answer:", result)

How to make it work**:**

  1. Run Visual Studio Code as an admin
  2. Create a virtual environment in VS Code: ‘>Python: Create Environment venv’
  3. Install packages: langchain and azure-ai-vision with pip install
  4. Add .env file and insert your OpenAI key there: OPENAI_API_KEY=sk-{…}
  5. Get your Azure Cognitive Service keys (it’s free) and add them in the code (os.environ variables)
  6. Update {id} placeholder in the Google Drive link to point to your file, or replace the whole link with yours. (the link should lead to the image file)
  7. Run you app💫

From the author :

📩 If you have any AI-related ideas or want to collaborate on something exciting, don't hesitate to reach out to me on LinkedIn