aslan-ng commited on
Commit
ce1933a
·
verified ·
1 Parent(s): c5f63f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -5
app.py CHANGED
@@ -712,10 +712,41 @@ system_prompt = f"""
712
  {instruction}
713
  """
714
 
715
- model = smolagents.OpenAIServerModel(
716
- model_id="gpt-4.1-mini",
717
- api_key=OPENAI_API,
718
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719
 
720
  agent = smolagents.CodeAgent(
721
  tools=[
@@ -769,7 +800,8 @@ with gr.Blocks() as demo:
769
  examples=[
770
  "Who is Ed?",
771
  "Who to talk to to create a wooden table?",
772
- "How can I access laser cutter?"
 
773
  ],
774
  inputs=[inp],
775
  )
 
712
  {instruction}
713
  """
714
 
715
+ client = openai.OpenAI(api_key=OPENAI_API)
716
+
717
+ class MyOpenAIChatModel:
718
+ def __init__(self, model_name: str = "gpt-4.1-mini"):
719
+ self.model_name = model_name
720
+
721
+ def __call__(self, messages, stop_sequences=None, **kwargs):
722
+ """
723
+ Adapter so smolagents' CodeAgent can call the OpenAI client.
724
+
725
+ CodeAgent passes `messages` as a list of either:
726
+ - dicts with 'role' and 'content'
727
+ - or Message-like objects with .role and .content
728
+ """
729
+ normalized = []
730
+ for m in messages:
731
+ if isinstance(m, dict):
732
+ normalized.append(m)
733
+ else:
734
+ role = getattr(m, "role", None)
735
+ content = getattr(m, "content", None)
736
+ if role is None or content is None:
737
+ raise ValueError(
738
+ f"Unexpected message type: {type(m)}; cannot extract role/content"
739
+ )
740
+ normalized.append({"role": role, "content": content})
741
+
742
+ resp = client.chat.completions.create(
743
+ model=self.model_name,
744
+ messages=normalized,
745
+ stop=stop_sequences,
746
+ )
747
+ return resp.choices[0].message.content
748
+
749
+ model = MyOpenAIChatModel("gpt-4.1-mini")
750
 
751
  agent = smolagents.CodeAgent(
752
  tools=[
 
800
  examples=[
801
  "Who is Ed?",
802
  "Who to talk to to create a wooden table?",
803
+ "How can I access the laser cutters?",
804
+ "How can I go to the welding space?"
805
  ],
806
  inputs=[inp],
807
  )